2012-08-13 105 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();同步持久化上下文的基础数据库。您的待处理查询应发送到数据库,但您仍可以完全回滚。

相关问题