我想人们可以总结出你的问题的答案.
几乎任何JPA操作都需要一个事务,除了不锁定实体的查找/选择(即任何不改变数据的JPA操作).
(JTA事务范围实体管理器)
对于JTA事务范围的实体管理器,最好从规范引用(第3章实体操作):
The persist, merge, remove, and refresh methods must be invoked within
a transaction context when an entity manager with a
transaction-scoped persistence context is used.
If there is no transaction context, the javax.persistence.TransactionRequiredException is thrown.
Methods that specify a lock mode other than LockModeType.NONE must be invoked
within a transaction context.
If there is no transaction context, the javax.persistence.TransactionRequiredException is thrown.
The find method (provided it is invoked without a lock or invoked with
LockModeType.NONE) and the getReference method are not required to be
invoked within a transaction context. If an entity manager with
transaction-scoped persistence context is in use, the resulting
entities will be detached; if an entity manager with an extended
persistence context is used, they will be managed. See section 3.3 for
entity manager use outside a transaction.
(应用程序管理/资源本地实体管理器)
对于应用程序管理的实体管理器,JPA规范不清楚该行为.在Hibernate的情况下,当不在事务内部时(它也可能依赖于JDBC驱动程序和DB连接的自动提交模式),发生的事情非常复杂.在这个主题上检查Hibernate’s article.基本上,强烈建议您始终使用事务进行上述操作.
对于问题的第二部分:如果你调用了一个托管实体的setter,并且没有刷新你分离它(即在事务提交之前),行为是不明确/未定义的,即你应该更好地纠正代码.
有缺陷的代码示例:
//begin Transaction
MyEntity entity = em.find(MyEntity.class, 1L);
entity.setField("New value");
em.detach();//it is not sure whether the "New value" will be persisted. To make sure it is persisted, ypu need to call em.flush() before detaching
//commit Transaction
通常,如果DB操作的顺序(与enity manager操作的顺序不同)不重要,您可以让JPA实现决定何时刷新(例如,在事务提交时).