简体   繁体   中英

Hibernate OGM with MongoDB persistence exception

I fighing with hibernate. I'm trying to follow hibernate.org/ogm/documentation/getting-started/ . Here are my files - 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="ogm-jpa-tutorial" transaction-type="JTA">
    <!-- Use Hibernate OGM provider: configuration will be transparent -->
    <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    <class>com.mycompany.hibernate.MyEntity</class>
    <properties>
      <property name="hibernate.ogm.datastore.provider" value="MONGODB"/>
      <!-- property optional if you plan and use Infinispan, otherwise adjust to your favorite
                NoSQL Datastore provider.
            <property name="hibernate.ogm.datastore.provider"
                      value="org.hibernate.ogm.datastore.infinispan.impl.InfinispanDatastoreProvider"/>
            -->
      <!-- defines which JTA Transaction we plan to use -->
      <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
    </properties>
  </persistence-unit>
</persistence>

entity class:

@Entity
@Table(name = "USstates")
public class MyEntity implements Serializable{
   @Id 
   String _id;
   String city;
   Integer pop;
  String state;
  public String getId(){return _id;}
  public String getCity(){return city;}
  public Integer getPop(){return pop;}
  public String getState(){return state;}

} 

application

private static final String JBOSS_TM_CLASS_NAME = "com.mycompany.hibernate.TransactionManager";
    public static void main( String[] args ) throws Exception
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    "ogm-jpa-tutorial");

TransactionManager tm = getTransactionManager();
//Persist entities the way you are used to in plain JPA
tm.begin();

EntityManager em = emf.createEntityManager();
MyEntity e = new MyEntity();
e = new MyEntity();
//Retrieve your entities the way you are used to in plain JPA
tm.begin();
em = emf.createEntityManager();
e = em.find(MyEntity.class, "0");
em.flush();
em.close();
tm.commit();

emf.close();

    }

        public static TransactionManager getTransactionManager() {
try {
Class<?> tmClass = App.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;
}

I have database test with collection USstates taken from http://media.mongodb.org/zips.json

Using thic codes I got some wornings about logs and what more important exception while running this application:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ogm-jpa-tutorial] Unable to build Hibernate SessionFactory

any ideas?

UPDATE:

after fighting i got an error of non exising class, I already tried milions of combinations of dependency and still got nothing... error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin

UPDATE 2.0: Now I have other problem:

Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.getIdentifierGeneratorFactory()Lorg/hibernate/id/factory/spi/MutableIdentifierGeneratorFactory;
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1119)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:291)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:373)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
    at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:92)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.mycompany.hibernate.App.main(App.java:48)

Ok silly question - did you remove the specific connection properties from your persistence.xml here, or did you not have them - example below?

    <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
    <property name="hibernate.ogm.datastore.database" value="rntsmpl"/>
    <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
    <property name="hibernate.ogm.datastore.port" value="59541"/>
    <property name="hibernate.ogm.datastore.username" value="admin"/>
    <property name="hibernate.ogm.datastore.password" value="nnTdE2jPf4FV"/>
    <property name="hibernate.transaction.jta.platform"    value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM