postgresql JPA 刷新与提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28392747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA flush vs commit
提问by monterico
in JPA, if we call EntityTransaction.commit(), does it automatically call EntityManager.flush()? or should we call them both? what is the difference? because i have problem with JPA, when i insert an entity to database, i call persist(). in the database, the data has been inserted (can be fetched), but that data doesn't show up in my app (i fetch it using findAll()). and on another entity, it showed up. is there something i don't know? i'm using standard Spring CRUD, JPA resource_local, and postgresql. sorry for my english, thanks in advance
在JPA中,如果我们调用EntityTransaction.commit(),它会自动调用EntityManager.flush()吗?还是我们应该同时称呼它们?有什么区别?因为我对 JPA 有问题,所以当我向数据库插入一个实体时,我调用了 persist()。在数据库中,数据已插入(可以获取),但该数据没有显示在我的应用程序中(我使用 findAll() 获取它)。在另一个实体上,它出现了。有什么我不知道的吗?我正在使用标准的 Spring CRUD、JPA resource_local 和 postgresql。对不起我的英语,提前致谢
回答by silentprogrammer
if we call EntityTransaction.commit(), does it automatically call EntityManager.flush()?
如果我们调用EntityTransaction.commit(),它是否会自动调用EntityManager.flush()?
Yes
是的
what is the difference?
有什么区别?
In flush() the changes to the data are reflected in database after encountering flush, but it is still in transaction.flush() MUST be enclosed in a transaction context and you don't have to do it explicitly unless needed (in rare cases), when EntityTransaction.commit() does that for you.
在flush() 中,在遇到flush 后,对数据的更改会反映在数据库中,但它仍在事务中。flush() 必须包含在事务上下文中,除非需要,否则您不必显式执行(在极少数情况下) ),当 EntityTransaction.commit() 为您执行此操作时。
回答by user2081279
If you have a @Version annotated column in your entity and call entityManager.flush(), then you will either (immediately!) get an OptimisticLockException, or the database will lock this row (or table). In the later case you can still call setRollbackOnly(), and the lock will later be released without a DB change.
如果您的实体中有一个@Version 注释的列并调用 entityManager.flush(),那么您将(立即!)得到一个 OptimisticLockException,或者数据库将锁定该行(或表)。在后一种情况下,您仍然可以调用 setRollbackOnly(),并且稍后将在不更改数据库的情况下释放锁。
Or from a different perspective, with flush() you can create a (pessimistic) lock on that database row. The others will still see the old entry, but if they try to update they will be blocked, until the lock is released.
或者从不同的角度来看,使用 flush() 您可以在该数据库行上创建一个(悲观的)锁。其他人仍然会看到旧条目,但如果他们尝试更新,他们将被阻止,直到锁定被释放。
All this is also true for CMT (container managed transactions). Instead of waiting for the moment, where the service method is finished and the CMT commit is performed, you can call flush() (even several times) in your service method and handle the OptimisticLockException(s) immediately.
所有这些对于 CMT(容器管理事务)也是如此。无需等待服务方法完成并执行 CMT 提交的时刻,您可以在服务方法中调用 flush()(甚至多次)并立即处理 OptimisticLockException(s)。
回答by Sanchi Girotra
em.flush()- It saves the entity immediately to the database with in a transaction to be used further and it can be rolled back.
em.flush()- 它立即将实体保存到数据库中,并在事务中进一步使用,并且可以回滚。
em.getTransaction().commit- It marks the end of transaction and saves all the chnages with in the transaction into the database and it can't be rolled back.
em.getTransaction().commit- 它标志着事务的结束并将事务中的所有更改保存到数据库中,并且不能回滚。
Refer https://prismoskills.appspot.com/lessons/Hibernate/Chapter_14_-_Flush_vs_Commit.jsp
参考https://prismoskills.appspot.com/lessons/Hibernate/Chapter_14_-_Flush_vs_Commit.jsp