使用 hibernate :
@PersistenceUnit(unitName = "oracle")
private EntityManagerFactory emf;
@Resource
private UserTransaction u;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EntityManager em = emf.createEntityManager();
try {
u.begin();
em.persist(some entity);
u.commit();
} catch (Exception e) {
e.printStackTrace();
}
这不会向数据库写入任何内容,如果我切换到 EclipseLink,它就可以正常工作。
如果我使用
EntityTransaction et = em.getTransaction();
而不是UserTransaction
hibernate 对数据库的写入。 (因此,hibernate 无法像 EclipseLink 那样看到 JTA)。
hibernate 有什么问题? (4.0.0-最终版)
谢谢
编辑:
我添加了最后 2 个属性:(我正在使用 glassfish)
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup" />
</properties>
但还是不行
最佳答案
我删除了最后两个属性,因为它们不是必需的。
我的问题是我在 u.begin()
之外创建 EM,我应该在事务内部创建它或调用 em.joinTransaction()
。
谢谢:
我从这里得到了答案: Hibernate JPA with JTA and Glassfish Application Server doesn't seem to commit
关于hibernate - 为什么在 Servlet 中使用 UserTransaction 时 Hibernate 不写入更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10676928/