如何使用 JPA 在 Hibernate 中启用 FlushModeType.COMMIT

How enable FlushModeType.COMMIT in Hibernate with JPA

我正在将 JPA 应用程序从旧版本的 EclipseLink 迁移到 Hibernate 5.4。9.Final。

在 EclipseLink 中,EntityManager FlushMode 通过 persistence.xml 中的以下 属性 配置为 COMMIT

<property name="eclipselink.persistence-context.flush-mode" value="COMMIT"/>

我尝试在 Hibernate 中做同样的事情,根据文档,我使用以下 属性:

<property name="org.hibernate.flushMode" value="COMMIT" />

但是休眠不接受它。

我不得不提的是行为需要保持不变,否则我应该重写整个应用程序。我的 persistence.xml 看起来像这样:

...
<persistence-unit name="test">
  <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  <!--<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>-->

  <class>com.test.Person</class>

  <properties>
    <property name="org.hibernate.flushMode" value="COMMIT"/>
    <!--<property name="eclipselink.persistence-context.flush-mode" value="COMMIT"/>-->
  </properties>
</persistence-unit>
...

在我的 JUnit 测试中,我 bootstrap JPA 如下:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test", properties);
EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction().begin();

Session session = entityManager.unwrap(Session.class);
System.out.println("Underlying Hibernate session flushmode #######         " + session.getFlushMode());
System.out.println("EntityManager flushmode                #######         " + entityManager.getFlushMode());

如果我 运行 使用 EclipseLink 进行测试,它会正确显示 EntityManager flushmode COMMIT。如果我 运行 使用 Hibernate 进行测试,EntityManager flushmode 会错误地显示 AUTO.

我做错了什么?

提前谢谢你, 麦克

看起来这个功能在 HHH-13565 下被破坏了。它在休眠 5.4.4 中工作。

这是HHH-13677