未找到org.hibernate.resource.transaction.TransactionCoordinatorBuilder类。我正在尝试使用mongodb

时间:2016-08-24 09:36:42

标签: hibernate hibernate-ogm

我是hibernate ogm的新手。我尝试了一个给出异常的小例子

  public static void main(String[] args) {
TransactionManager transactionManager=com.arjuna.ats.jta.TransactionManager.transactionManager();       
        try {
            transactionManager.begin();

            OgmSessionFactory sessionFactory=HibernateUtil.getSessionFactory();
          Session session=sessionFactory.openSession();
            Testing testing=new Testing();
            testing.setId(1l);
            testing.setfName("dkd");
            testing.setlName("dash");

            session.save(testing);
            transactionManager.commit();

        } catch (Exception e) {
            // TODO: handle exception
            try {
                //transactionManager.rollback();
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
}`

这是我的日志..

  
    

2016年8月24日下午2:50:35 com.arjuna.ats.arjuna.recovery.TransactionStatusManager addService     信息:ARJUNA012163:启动服务     com.arjuna.ats.arjuna.recovery.ActionStatusService on port 52455 Aug     24,2016 2:50:36 PM     com.arjuna.ats.internal.arjuna.recovery.TransactionStatusManagerItem      信息:ARJUNA012337:TransactionStatusManagerItem主机:     127.0.0.1 port:52455 Aug 24,2016 2:50:36 PM com.arjuna.ats.arjuna.recovery.TransactionStatusManager start INFO:     ARJUNA012170:TransactionStatusManager在端口52455和主机上启动     127.0.0.1与服务com.arjuna.ats.arjuna.recovery.ActionStatusService 2016年8月24日     下午2:50:36 org.hibernate.Version logVersion INFO:HHH000412:Hibernate     Core {5.2.2.Final} 2016年8月24日下午2:50:36     org.hibernate.cfg.Environment信息:HHH000206:     hibernate.properties未找到2016年8月24日下午2:50:36     org.hibernate.cfg.Environment buildBytecodeProvider INFO:HHH000021:     字节码提供程序名称:线程“main”中的javassist异常     java.lang.NoClassDefFoundError:     org / hibernate / resource / transaction / TransactionCoordinatorBuilder at     org.hibernate.ogm.boot.impl.OgmServiceRegistryInitializer.contribute(OgmServiceRegistryInitializer.java:71)       在     org.hibernate.boot.registry.StandardServiceRegistryBuilder.applyServiceContributors(StandardServiceRegistryBuilder.java:305)       在     org.hibernate.boot.registry.StandardServiceRegistryBuilder.build(StandardServiceRegistryBuilder.java:273)       在     org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)       在     org.hibernate.ogm.cfg.OgmConfiguration.buildSessionFactory(OgmConfiguration.java:33)       在utils.HibernateUtil.getSessionFactory(HibernateUtil.java:19)at     hibernatePrac.TestApp.main(TestApp.java:20)引起:     抛出java.lang.ClassNotFoundException:     org.hibernate.resource.transaction.TransactionCoordinatorBuilder at     java.net.URLClassLoader.findClass(URLClassLoader.java:381)at     java.lang.ClassLoader.loadClass(ClassLoader.java:424)at     sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)at at     java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 7更多

  

的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Practice</groupId>
    <artifactId>maven</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.2.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.2.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <!-- https://mvnrepository.com/artifact/org.jboss/jboss-transaction-spi -->
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-transaction-spi</artifactId>
            <version>7.1.0.Final</version>
        </dependency>
        <!-- Hibernate OGM dependency -->
        <!-- https://mvnrepository.com/artifact/org.hibernate.ogm/hibernate-ogm-core -->
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-core</artifactId>
            <version>5.0.1.Final</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>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <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>
        <!-- JBoss Transactions dependency -->
        <dependency>
            <groupId>org.jboss.jbossts</groupId>
            <artifactId>jbossjta</artifactId>
            <version>4.16.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-mongodb</artifactId>
            <version>5.0.0.CR1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.5.4</version>
        </dependency>


        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> 
            <version>1.2.17</version> </dependency> -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.0</version>
        </dependency>


    </dependencies>
    <build>
        <finalName>maven</finalName>
    </build>
</project>

的persistence.xml

<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="org.hibernate.ogm.tutorial.jpa" transaction-type="JTA">
        <!-- Use Hibernate OGM provider: configuration will be transparent -->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <properties>
        <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
            <property name="hibernate.ogm.datastore.provider" value="mongodb" />

            <!-- defines which JTA Transaction we plan to use -->
            <property name="hibernate.ogm.datastore.provider" value="mongodb" />
            <property name="hibernate.ogm.datastore.database" value="test" />
            <property name="hibernate.ogm.datastore.host" value="localhost" />
            <property name="hibernate.ogm.datastore.port" value="27017" />
            <property name="hibernate.ogm.datastore.username" value="dkd" />
            <property name="hibernate.ogm.datastore.password" value="dkd123" />
        </properties>
    </persistence-unit>
</persistence>`

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.ogm.datastore.provider">mongodb</property>
        <property name="hibernate.ogm.datastore.database">test</property>
        <property name="hibernate.ogm.datastore.host">127.0.0.1</property>
        <property name="hibernate.ogm.datastore.port">27017</property>
        <property name="hibernate.ogm.datastore.username">dkd</property>
        <property name="hibernate.ogm.datastore.password">dkd123</property>
        <!--  <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
         <property name="hibernate.connection.datasource">java:/DefaultDS</property> 
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>
        <property name="hibernate.transaction.auto_close_session">true</property> -->
        <property name="hibernate.dialect">org.hibernate.ogm.datastore.mongodb.MongoDBDialect</property>
    </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

Hibernate OGM与Hibernate ORM 5.2不兼容(正在进行PR以使其与ORM 5.1兼容,但最新发布的版本仅与ORM 5.0兼容)。

你需要为你的hibernate-core和hibernate-entitymanager依赖使用Hibernate ORM 5.0.10.Final。

话虽如此,如果它只是一个简单的例子,那么让hibernate-ogm通过其传递依赖来获得正确的依赖:它将避免你出现这类问题。