休眠 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>

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文档中找到其他解释:关于刷新和事务.

相关内容

最新更新