我將數據插入到SQL Server表中。在插入期間,我需要檢查是否有任何現有行正在更新,然後更新另一個表,以更新哪一列。對於前:
表1:新行插入A1和行更新A2
表2:兩行應創建如何在hibernate中將數據寫入表中@EntityListener使用Camel的操作JPA
<Old Value> <New Value> <Unique Key>
"" A1 PK1 --for insert
"Dummy Val" A2 PK2 --for update
我使用Apache的駱駝,休眠,JPA
主表中插入數據:
@Entity(name = "tableName")
@EntityListeners(value = updateEntityListener.class)
public class ExampleTable{
@Column(name = "col1")
private int col1;
//getter setter
@Override
public String toString() {
return "data";
}
監聽
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
public class updateEntityListener{
private String preUpdate = "";
private String postUpdate = "";
@PostPersist
public void postPersist(final ExampleTable ex) {
//Insert data in another table
System.out.println("Row inserted");
}
@PostUpdate
public void postUpdate(final ExampleTable ex) {
postUpdate = ex.toString();
if (!preUpdate.equals(postUpdate)) {
System.out.println("Row Updated");
// Insert in new table, compare from postLoad
}
}
@PostLoad
public void postLoad(final ExampleTable ex) {
preUpdate = ex.toString();
}
}
駱駝生成器(Java的DSL)
from("timer:invoke?repeatCount=1")
.log("Processing ${id}")
.toD("https4://URL")
.inheritErrorHandler(true)
.unmarshal()
.json(JsonLibrary.Jackson)
.split(body())
.parallelProcessing()
.process(new MyProcessor())
.to("jpa:ExampleTable")
我需要在那裏的代碼,以及如何編寫將數據寫入表2,但不能確定。任何幫助表示讚賞。如果這個問題已經被問到,請直接指向那個鏈接。