0

我有一个数据库表“MyTable”,它在更新字段“状态”时触发。

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

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”进行更改以触发更新?

-谢谢

4

3 回答 3

1

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

于 2012-09-18T16:08:56.157 回答
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();
于 2012-08-14T11:36:38.410 回答
0

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

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

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

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

于 2012-09-19T13:56:24.827 回答