user1300991
user1300991

Reputation: 45

How enable FlushModeType.COMMIT in Hibernate with JPA

I'm migrating a JPA application from an old version of EclipseLink to Hibernate 5.4.9.Final.

In EclipseLink, the EntityManager FlushMode was configured to COMMIT, via the following property in persistence.xml:

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

I try to do the same in Hibernate, and as per the documentation, I use the following property:

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

but hibernate does not pick it up.

I have to mention that the behaviour needs to remain the same, otherwise I should rewrite the whole application. My persistence.xml looks like this:

...
<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>
...

In my JUnit Test, I bootstrap JPA as follows:

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());

If I run the test with EclipseLink, it shows the EntityManager flushmode COMMIT correctly. If I run the test with Hibernate, the EntityManager flushmode incorrectly shows AUTO.

What am I doing wrong?

Thank you in advance, Mike

Upvotes: 1

Views: 488

Answers (1)

SternK
SternK

Reputation: 13041

It looks like this functionality was broken under HHH-13565. It works in the hibernate 5.4.4.

This is HHH-13677

Upvotes: 1

Related Questions