Search code examples
javahibernatehibernate-ogm

Hibernate OGM doesn't persist entities without transaction


I've just reinstalled Eclipse on another machine and imported a project that used to work (it was saving entites in mongodb without transaction).

The code is just this

    MyEntity ent = new MyEntity();
    ent.setTitle("title");
    EntityManager e = Persistence.createEntityManagerFactory("MongoPU").createEntityManager();
    e.persist(event);

It persist this entity if I commit an Arjuna JTA transaction. But I'm wondering why this code used to work without transaction too.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MongoPU" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
            <property name="com.arjuna.ats.jta.jtaTMImplementation"
                value="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple" />
            <property name="com.arjuna.ats.jta.jtaUTImplementation"
                value="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple" />
            <property name="hibernate.ogm.datastore.create_database"
                value="true" />
            <property name="hibernate.ogm.datastore.provider"
                value="mongodb" />
            <property name="hibernate.ogm.datastore.database"
                value="mongodata" />
            <property name="hibernate.ogm.mongodb.host"
                value="127.0.0.1" />
            <property name="hibernate.ogm.mongodb.port" value="27017" />
        </properties>
    </persistence-unit> 

</persistence>

the part of the pom related to Hibernate

    <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-mongodb</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.jbossts</groupId>
            <artifactId>jbossjta</artifactId>
            <version>4.16.6.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
  </dependency>

Not sure it's related but I'm pretty sure I neven had this Warning at runtime (please note I always used Hibernate OGM 5.1.0, NO changes to pom)

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by javassist.util.proxy.SecurityActions (file:/home/aantonio/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of javassist.util.proxy.SecurityActions
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Solution

  • I don't know why it was working before but the point of OGM is that you should use transaction demarcation in your app. If you don't want to use it, you need to flush() the operations when you are ready.

    You can find additional explanations in the Hibernate OGM documentation: On flush and transactions .