简体   繁体   English

JPA Hibernate 交易问题

[英]JPA Hibernate transaction issue

I accidentally discoverd that I can persist changes on a object, even if I dont write them inside the transaction.我偶然发现我可以在 object 上保留更改,即使我没有将它们写入事务中。 I wounder how this happend, cuz in theory, I should'nt be able to change the age value in the database, if I dont write the changes inside the transaction.我很清楚这是怎么发生的,因为理论上,如果我不在事务中写入更改,我不应该能够更改数据库中的年龄值。 PS: If I remove the last 2 lines, it does nothing to the db, as expected. PS:如果我删除最后两行,它对数据库没有任何作用,正如预期的那样。

var emf = Persistence.createEntityManagerFactory("java2c2PU");
var em = emf.createEntityManager();
var p = em.find(Pisica.class, 5);

p.setAge(5);

em.getTransaction().begin();
    
em.getTransaction().commit();

You are in the EXTENDED JPA mode.您处于扩展 JPA 模式。 So the Entity Manager is not bound to the transaction.所以实体管理器没有绑定到事务。

Changes are preserved in the Persistence Context.更改保留在持久性上下文中。 And as soon you commit a transaction the changes are flushed and committed.一旦您提交事务,更改就会被刷新并提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM