簡體   English   中英

使用 hibernate 在不同的應用程序上更新同一個表

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

我有 2 個應用程序節點,它們都更新同一個數據庫表。 但是,更新的列是不同的。 在這里出現問題,然后運行應用程序更新整行而不是單列。 讓我們深入研究問題解釋。

表名是“student”,它有 3 列。 假設 ID,column1,column2

行的初始 state 為 ID=1, column1="abc", column2="xyz"有問題的場景步驟如下

  • App-instance-1:讀取行並將其保存在持久性 state 中。
  • App-instance-2:讀取行並將其保存在持久性 state 中。
  • App-instance-1:將 pojo 屬性 column1 更新為“ABC”( column1="ABC" , column2="xyz")。
  • App-instance-2:將 pojo 屬性 column2 更新為“XYZ”(column1="abc", column2="XYZ" )。
  • app-instance-1:將pojo合並到db表中,行看起來像ID=1,column1="ABC",column2="xyz"
  • app-instance-2:將pojo合並到db表中,行看起來像ID=1,column1="abc",column2="XYZ"

最后,應用實例覆蓋彼此的列。 我想將最后一行視為column1="ABC", column2="XYZ" 但是,它是column1="abc", column2="XYZ"

我的問題是,為什么 App-instance-1 會更新 column2,即使 column2 沒有更改。 column2 在 instance-1 上不臟。 我該如何克服這個問題?

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

提前致謝。

據我所知,您可以嘗試以下兩種方法之一:

  • 使用樂觀鎖定(例如 Hibernate 提供的@Version
  • 創建自定義查詢以僅更新一個字段,而不是更新整個實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM