我和hibernate一起玩。我正在尝试关注hibernate.org/ogm/documentation/getting-started/。这是我的文件 - 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
@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;}
}
应用
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;
}
我从http://media.mongodb.org/zips.json
获取了收集USstates的数据库测试使用thic代码我在运行此应用程序时遇到了一些关于日志和更重要的异常的问题:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ogm-jpa-tutorial] Unable to build Hibernate SessionFactory
任何想法?
更新:
战斗结束后,我得到了一个非退出课程的错误,我已经尝试了数百万的依赖组合,但仍然没有...错误:线程“main”中的异常java.lang.NoClassDefFoundError:org / hibernate / util / xml / Origin
更新2.0: 现在我还有其他问题:
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)
答案 0 :(得分:0)
好的愚蠢的问题 - 您是否从此处删除了persistence.xml中的特定连接属性,或者您没有它们 - 下面的示例?
<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" />