2012-08-13 101 views
0

我有一個數據庫表'MyTable',它在更新字段'Sta​​tus'時具有觸發器。EJB3.0更新表中具有相同值的字段

下面是我想要做一個假碼:

MyTable table = new Mytable(); 
table.setTableId(1); 
table.setStatus ("NEW"); 
em.persist (table); //At this point the trigger did not kick in since this inserted a new record 

... 

MyTable table2 = em.find(MyTable.class, 1); 
table2.setStatus ("NEW"); 
em.merge(table2)//Even though im updating the record with the same status with the same value, i still want the trigger to kick. However the trigger is not being activated. 


... 

MyTable table3 = em.find(MyTable.class, 1); 
table3.setStatus ("OLD"); 
em.merge(table3)//The trigger is being activated here since the status is different the status value when it was inserted the first time. 

長話短說,我怎樣才能做到「transfer2」的變化觸發更新,即使狀態一樣?

-Thanks

回答

0

JPA不更新沒有更改的對象。

你可以嘗試改變其他東西(刷新),然後改回它。

您也可以使用JPQL更新查詢來更新它。

取決於你的JPA提供者,你可以強迫它更新沒有改變的字段,但這會導致非常糟糕的性能。

0

能否請您嘗試更新enity並提交事務,而不使用合併。

em.getTransaction().begin(); 
MyTable table2 = em.find(MyTable.class, 1); 
table2.setStatus ("NEW"); 

//em.merge(table2)//Even though im updating the record with the same status with the 
// same value, i still want the trigger to kick. However the trigger is not being activated. 
em.getTransaction().commit(); 
+0

你好,問題是在同一個事務中還有其他操作。因此,我希望能夠在發生錯誤時回滾所有數據庫操作,因此提交事務不是一個選項。 – Brams 2012-08-17 07:56:42

1

使用em.flush();同步持久化上下文的基礎數據庫。您的待處理查詢應發送到數據庫,但您仍可以完全回滾。

相關問題