0

我正在尝试使文档中的 Dog/Breed 示例适用于 Neo4j 持久性。数据库正常创建,但数据无法刷新,至少看起来如此。我在em.flush()指令(在上面的 DogBreedRunner.java 类中)中收到错误org.neo4j.graphdb.NotInTransactionException 。

我用谷歌搜索并解决了这个问题,但真的无法解决它。这是我的项目(我没有接触文档中的 java 类)。

有人可以帮忙吗?让我知道是否需要其他信息。

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.0">

    <persistence-unit name="ogm-jpa-tutorial" transaction-type="JTA">
        <!-- Use Hibernate OGM provider: configuration will be transparent -->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <properties>
            <property name="hibernate.ogm.datastore.provider" value="neo4j_embedded" />
            <property name="hibernate.ogm.neo4j.database_path" value="C:/Neo4j/NEO4J_HOME/data/graph.db" />

            <!-- defines which JTA Transaction we plan to use -->
            <property name="hibernate.transaction.jta.platform"
                      value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

狗.java

package org.hibernate.ogm.examples.gettingstarted.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.TableGenerator;

@Entity
public class Dog {
   @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "dog")
   @TableGenerator(
      name = "dog",
      table = "sequences",
      pkColumnName = "key",
      pkColumnValue = "dog",
      valueColumnName = "seed"
   )
   public Long getId() { return id; }
   public void setId(Long id) { this.id = id; }
   private Long id;

   public String getName() { return name; }
   public void setName(String name) { this.name = name; }
   private String name;

   @ManyToOne
   public Breed getBreed() { return breed; }
   public void setBreed(Breed breed) { this.breed = breed; }
   private Breed breed;
}

面包.java

package org.hibernate.ogm.examples.gettingstarted.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Breed {

   @Id @GeneratedValue(generator = "uuid")
   @GenericGenerator(name="uuid", strategy="uuid2")
   public String getId() { return id; }
   public void setId(String id) { this.id = id; }
   private String id;

   public String getName() { return name; }
   public void setName(String name) { this.name = name; }
   private String name;
}

DogBreedRunner.java

package org.hibernate.ogm.examples.gettingstarted;

import org.hibernate.ogm.examples.gettingstarted.domain.Breed;
import org.hibernate.ogm.examples.gettingstarted.domain.Dog;
import org.hibernate.ogm.util.impl.Log;
import org.hibernate.ogm.util.impl.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.TransactionManager;
import java.lang.reflect.InvocationTargetException;

public class DogBreedRunner {

    private static final String JBOSS_TM_CLASS_NAME = "com.arjuna.ats.jta.TransactionManager";
    private static final Log logger = LoggerFactory.make();

    public static void main(String[] args) {

        TransactionManager tm = getTransactionManager();

        //build the EntityManagerFactory as you would build in in Hibernate Core
        EntityManagerFactory emf = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );

        //Persist entities the way you are used to in plain JPA
        try {
            tm.begin();
            logger.infof( "About to store dog and breed" );
            EntityManager em = emf.createEntityManager();
            Breed collie = new Breed();
            collie.setName( "Collie" );
            em.persist( collie );
            Dog dina = new Dog();
            dina.setName( "Dina" );
            dina.setBreed( collie );
            em.persist( dina );
            Long dinaId = dina.getId();
                        logger.infof( "About to commit" );
            em.flush();
            em.close();
            tm.commit();

            //Retrieve your entities the way you are used to in plain JPA
            logger.infof( "About to retrieve dog and breed" );
            tm.begin();
            em = emf.createEntityManager();
            dina = em.find( Dog.class, dinaId );
            logger.infof( "Found dog %s of breed %s", dina.getName(), dina.getBreed().getName() );
            em.flush();
            em.close();
            tm.commit();

            emf.close();
        } catch ( Exception e ) {
            e.printStackTrace();
        }

    }

    public static TransactionManager getTransactionManager() {
        try {
            Class<?> tmClass = DogBreedRunner.class.getClassLoader().loadClass( JBOSS_TM_CLASS_NAME );
            return (TransactionManager) tmClass.getMethod( "transactionManager" ).invoke( null );
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        } catch ( InvocationTargetException e ) {
            e.printStackTrace();
        } catch ( NoSuchMethodException e ) {
            e.printStackTrace();
        } catch ( IllegalAccessException e ) {
            e.printStackTrace();
        }
        return null;
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>hibernate-ogm-documentation-examples</groupId>
    <artifactId>HibernateOMG</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>Full code for the Getting Started Guide</description>
    <url>http://ogm.hibernate.org</url>

    <dependencies>
        <!-- Hibernate OGM dependency -->
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-core</artifactId>
            <version>4.1.0.Beta5</version>
        </dependency>

        <!-- standard APIs dependencies - provided in a Java EE container -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-neo4j</artifactId>
            <version>4.1.0.Beta5</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            <version>1.0.0.Final</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
            <type>jar</type>
        </dependency>       

        <!-- JBoss Transactions dependency -->
        <dependency>
            <groupId>org.jboss.jbossts</groupId>
            <artifactId>jbossjta</artifactId>
            <version>4.16.4.Final</version>
        </dependency>


    </dependencies>
</project>

例外:

org.neo4j.graphdb.NotInTransactionException 在 org.neo4j.kernel.impl.persistence.PersistenceManager.getCurrentTransaction(PersistenceManager.java:284) 在 org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:249) 在在 org.neo4j.kernel.impl.core.ThreadToStatementContextBridge.transaction(ThreadToStatementContextBridge.java:53) 在 org.neo4j.kernel.impl 的 org.neo4j.kernel.impl.persistence.PersistenceManager.currentKernelTransactionForReading(PersistenceManager.java:230)。 core.ThreadToStatementContextBridge.instance(ThreadToStatementContextBridge.java:47) at org.neo4j.kernel.impl.core.NodeProxy.setProperty(NodeProxy.java:203) at org.hibernate.ogm.datastore.neo4j.Neo4jDialect.putTupleOperation(Neo4jDialect. java:373) 在 org.hibernate.ogm.datastore.neo4j.Neo4jDialect。applyOperation(Neo4jDialect.java:355) at org.hibernate.ogm.datastore.neo4j.Neo4jDialect.applyTupleOperations(Neo4jDialect.java:348) at org.hibernate.ogm.datastore.neo4j.Neo4jDialect.updateTuple(Neo4jDialect.java:123)在 org.hibernate.ogm.persister.OgmEntityPersister.insert(OgmEntityPersister.java:928) 在 org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104) 在 org.hibernate.engine.spi.ActionQueue.executeActions (ActionQueue.java:463) 在 org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349) 在 org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) 在 org.hibernate。 org.hibernate.internal.SessionImpl 中的 event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)。flush(SessionImpl.java:1222) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335) at org.hibernate.ogm.jpa.impl.OgmEntityManager.flush(OgmEntityManager.java:119) at org .hibernate.ogm.examples.gettingstarted.DogBreedRunner.main(DogBreedRunner.java:40)

4

1 回答 1

1

我将问题发布到官方论坛,我得到了正确的答案。

  • 从persistence.xml 中删除“hibernate.transaction.jta.platform”属性。这样做的原因是我们需要一个 Neo4j atm 的特殊实现。(这应该有望在未来改变)。但是 Hibernate OGM 将使用 JBossStandAloneJtaPlatform 作为除 Neo4j 之外的所有商店的默认值,因此无需指定它。
  • 使用以下方法检索事务管理器(需要在 EMF 引导后完成):
private static TransactionManager extractJBossTransactionManager(EntityManagerFactory factory) {
    SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) ( (HibernateEntityManagerFactory) factory).getSessionFactory();
     return sessionFactory.getServiceRegistry().getService( JtaPlatform.class ).retrieveTransactionManager(); 
}

你应该顺便说一句。使用同一个实体管理器连续处理多个事务,您需要在事务开始后调用 EntityManager#joinTransaction()。

来源:https ://forum.hibernate.org/viewtopic.php?f=31&t=1035764&p=2480105

于 2014-07-28T10:11:48.133 回答