2017-02-15 22 views
0

我將數據插入到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,但不能確定。任何幫助表示讚賞。如果這個問題已經被問到,請直接指向那個鏈接。

回答

0

我找到了解決方案,共享,以便其他人可以使用,如果卡住類似的情況。
爲第二個表中實體創建列

@Entity(name = "tableName") 
@EntityListeners(value = updateEntityListener.class) 
public class ExampleTable{ 

    @Column(name = "col1") 
    private int col1; 
    @Transient 
    private List<Table2> auditInfo = new ArrayList<Table2>(); 
    //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 
ex.setAuditInfo(new Object()); //Actual object instead of new Object() 
     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 
      ex.setAuditInfo(new Object()); 
     } 
    } 
    @PostLoad 
    public void postLoad(final ExampleTable ex) { 
     preUpdate = ex.toString(); 
    } 
} 

最後建設者變化:

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") 
     .process(new Processor() { 
       public void process(Exchange arg0) throws Exception { 
        List<Table2> info= ((ExampleTable) arg0.getIn().getBody()).getAuditInfo(); 
        if (null != info&& info.size() > 0) { 
         arg0.getIn().setBody(info); 
        }else{ 
         arg0.getIn().setBody(new ArrayList<Table2>()); 
        } 

       } 
      }) 
      .to("jpa:Table2?entityType=java.util.ArrayList"); 
0

我推薦使用一種非常類似於Envers實時審計的方法。

你所做的是註冊各種PostXXXEventListener實現以滿足您的需求,然後讓那些偵聽器根據當前正在處理的動作持久化新實體。

詳情請看org.hibernate.event.spi

相關問題