簡體   English   中英

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文檔中找到其他說明: 關於刷新和事務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM