EJB3.0使用相同的值更新表中的字段

时间:2012-08-13 15:27:38

标签: jpa ejb-3.0

我有一个数据库表'MyTable',在更新字段'Sta​​tus'时会触发。

下面是我正在尝试做的虚拟代码:

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

3 个答案:

答案 0 :(得分:1)

使用em.flush();将持久性上下文与基础数据库同步。您的待处理查询应发送到数据库,但您仍然可以完全回滚。

答案 1 :(得分:0)

请您尝试更新实体并提交交易,而不使用合并。

喜欢

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();

答案 2 :(得分:0)

JPA不会更新没有更改的对象。

您可以尝试更改为其他内容(刷新),然后将其更改回来。

您还可以使用JPQL更新查询来更新它。

根据您的JPA提供程序,您可能会强制它更新未更改的字段,但这会导致性能非常差。