Hibernate OGM 不会在没有事务的情况下保留实体

Hibernate OGM doesn't persist entities without transaction

我刚刚在另一台机器上重新安装了 Eclipse 并导入了一个曾经工作过的项目(它在 mongodb 中保存了没有 事务的实体)。

代码就是这样

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

如果我提交 Arjuna JTA 事务,它 会保留 这个实体。 但是我想知道为什么这段代码过去也可以在没有事务的情况下工作。

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>

pom中与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>

不确定是否相关,但我很确定我在运行时从未收到过此警告(请注意我一直使用 Hibernate OGM 5.1.0,没有更改 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

我不知道为什么它以前可以工作,但 OGM 的要点是你应该在你的应用程序中使用事务划分。如果您不想使用它,您需要flush()准备就绪后进行操作。

您可以在 Hibernate OGM 文档中找到更多解释:关于刷新和事务 .