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

时间:2019-01-22 00:31:06

标签: java hibernate hibernate-ogm

我刚刚在另一台计算机上重新安装了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>

与Hibernate相关的pom部分

    <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

1 个答案:

答案 0 :(得分:0)

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

您可以在Hibernate OGM文档中找到其他说明:On flush and transactions