简体   繁体   English

使用 hibernate 在不同的应用程序上更新同一个表

[英]Updating the same table on the different applications using hibernate

I have 2 nodes of application and both of them updates the same db table.我有 2 个应用程序节点,它们都更新同一个数据库表。 However, updated columns are different.但是,更新的列是不同的。 In here the problem occurs and secondly running application updates the whole row instead single column.在这里出现问题,然后运行应用程序更新整行而不是单列。 Let's deep dive into problem explanation.让我们深入研究问题解释。

The table name is "student" and it has 3 columns.表名是“student”,它有 3 列。 Assuming ID, column1,column2假设 ID,column1,column2

initial state of the row is ID=1, column1="abc", column2="xyz" The problematic scenario steps are below行的初始 state 为 ID=1, column1="abc", column2="xyz"有问题的场景步骤如下

  • App-instance-1: reads the row and keep it in persistent state. App-instance-1:读取行并将其保存在持久性 state 中。
  • App-instance-2: reads the row and keep it in persistent state. App-instance-2:读取行并将其保存在持久性 state 中。
  • App-instance-1: updates the pojo attribute column1 as to be "ABC" ( column1="ABC" , column2="xyz"). App-instance-1:将 pojo 属性 column1 更新为“ABC”( column1="ABC" , column2="xyz")。
  • App-instance-2: updates the pojo attribute column2 as to be "XYZ" (column1="abc", column2="XYZ" ). App-instance-2:将 pojo 属性 column2 更新为“XYZ”(column1="abc", column2="XYZ" )。
  • App-instance-1: merges the pojo into db table and the row looks like ID=1, column1="ABC", column2="xyz" app-instance-1:将pojo合并到db表中,行看起来像ID=1,column1="ABC",column2="xyz"
  • App-instance-2: merges the pojo into db table and the row looks like ID=1, column1="abc", column2="XYZ" app-instance-2:将pojo合并到db表中,行看起来像ID=1,column1="abc",column2="XYZ"

Finally, App-instances override the each other' s columns.最后,应用实例覆盖彼此的列。 I want to see the final row as column1="ABC", column2="XYZ" .我想将最后一行视为column1="ABC", column2="XYZ" However, it is column1="abc", column2="XYZ" .但是,它是column1="abc", column2="XYZ"

My question is that why App-instance-1 updates the column2 even if no changes on the column2.我的问题是,为什么 App-instance-1 会更新 column2,即使 column2 没有更改。 column2 is not dirty on the instance-1. column2 在 instance-1 上不脏。 How can i overcome this issue?我该如何克服这个问题?

@Entity
@Table("student")
public class Student {
    private Long ID;
    private String column1;
    private String column2;
    
    // Assuming other definations are ok.
}

Thanks in advance.提前致谢。

As far as my knowledge goes you could try one of those 2 methods:据我所知,您可以尝试以下两种方法之一:

  • Use optimistic locking (ex. @Version provided by Hibernate)使用乐观锁定(例如 Hibernate 提供的@Version
  • Create custom queries to update only one field instead of updating whole entity.创建自定义查询以仅更新一个字段,而不是更新整个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠中同一表的不同表示 - Different representation of same table in hibernate 使用Hibernate从两个不同的Web应用程序(GXT)连接同一数据库 - Connecting the same database from two different web applications(GXT) using Hibernate 2 使用不同 hibernate 版本但相同 oracle 数据库的应用程序抛出唯一约束错误 - 2 Applications using different hibernate versions but same oracle database throwing unique constraint error 为什么关联表无法使用Hibernate正确更新? - Why Associated table not updating properly using hibernate? 使用Hibernate更新表中“许多”项的计数 - Updating the count of 'many' items in a Table using Hibernate 同一个表中不同项目的休眠映射 - Hibernate mapping for different items in the same table 并发读取-使用JPA Hibernate通过不同的Java应用程序实例从同一表读取不同的行 - Concurrent Read - Reading different rows from same table by different java application instance in using jpa hibernate 使用Hibernate将两个或多个应用程序连接到同一个数据库 - Connect two or more applications to the same database using Hibernate 两个应用程序在Hibernate Search中使用相同的索引文件 - Two Applications using the same index file with Hibernate Search 使用相同的jenkins作业在不同的服务器中部署不同的应用程序 - Using same jenkins job for deployment of different applications in different servers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM