我尝试使用Hibernate OGM为mongoDB配置我的Spring Web应用程序,使用java 1.6和OGM 4.1.3.Final。
对于Session我编写HibernateUtil.java,它可以正常使用JUnit测试,但它会抛出异常。
Hibernate.java
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.ogm.cfg.OgmConfiguration;
public class HibernateUtil {
private static final Logger log = Logger.getLogger(HibernateUtil.class.getName());
private static final SessionFactory sessionFactory;
private static final StandardServiceRegistry serviceRegistry;
static {
try {
// create a new instance of OmgConfiguration
OgmConfiguration cfgogm = new OgmConfiguration();
// process configuration and mapping files
cfgogm.configure("hibernate_mongoDB.cfg.xml");
// create the SessionFactory
//cfgogm.getProperties();
serviceRegistry = (StandardServiceRegistry) new StandardServiceRegistryBuilder()
.applySettings(cfgogm.getProperties()).build();
sessionFactory = cfgogm.buildSessionFactory(serviceRegistry);
} catch (Exception ex) {
log.log(Level.SEVERE, "Initial SessionFactory creation failed !",
ex);
throw new EdumissException(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
TestMongoDb.java
package mkcl.os.test;
import java.util.Date;
import java.util.List;
import mkcl.os.apps.edumiss.utilities.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
public class TestMongoDB {@
Test
public void testMongoDB() {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Players player = new Players();
player.setId("2");
player.setAge((short) 24);
player.setBirth(new Date());
player.setName("Pankaj");
player.setSurname("Saboo");
session.beginTransaction();
session.save(player);
session.getTransaction().commit();
String hql = "FROM Players";
Query query = session.createQuery(hql);
List results = query.list();
//List results = cr.list();
for (Object object: results) {
System.out.println("Data : " + object);
}
//session.flush(); // flush happens automatically anyway
} catch (RuntimeException re) {
session.getTransaction().rollback();
throw re;
} finally {
session.close();
}
}
}
Players.java
package mkcl.os.test;
import java.util.Date;
public class Players {
private String id;
@Override
public String toString() {
return "Players [id=" + id + ", name=" + name + ", surname=" + surname
+ ", age=" + age + ", birth=" + birth + ", getId()=" + getId()
+ ", getName()=" + getName() + ", getSurname()=" + getSurname()
+ ", getAge()=" + getAge() + ", getBirth()=" + getBirth()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ ", toString()=" + super.toString() + "]";
}
private String name;
private String surname;
private short age;
private Date birth;
public Players() {
super();
// TODO Auto-generated constructor stub
}
public Players(String id, String name, String surname, short age, Date birth) {
super();
this.id = id;
this.name = name;
this.surname = surname;
this.age = age;
this.birth = birth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
Players.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 5, 2014 4:06:20 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="mkcl.os.test.Players" table="PLAYERS">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="surname" type="java.lang.String">
<column name="SURNAME" />
</property>
<property name="age" type="short">
<column name="AGE" />
</property>
<property name="birth" type="java.util.Date">
<column name="BIRTH" />
</property>
</class>
</hibernate-mapping>
hibernate_mongoDB.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> -->
<!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.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.ogm.datastore.provider">mongodb</property>
<!-- <property name="hibernate.ogm.datastore.grid_dialect">org.hibernate.ogm.datastore.mongodb.MongoDBDialect</property> -->
<property name="hibernate.ogm.datastore.database">yourdb</property>
<property name="hibernate.ogm.datastore.host">localhost</property>
<property name="hibernate.ogm.datastore.port">27017</property>
<property name="hibernate.ogm.datastore.create_database">true</property>
<mapping resource="mkcl/os/test/Players.hbm.xml"/>
</session-factory>
</hibernate-configuration>
的pom.xml
用于OGM的依赖
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-core</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>4.1</version>
</dependency>
在Tomcat上部署之后,我正在通过另一个jsp,HibernateUtil.java在下面的行中抛出异常
OgmConfiguration cfgogm = new OgmConfiguration();
异常行是
java.lang.IncompatibleClassChangeError:class org.parboiled.transform.ClassNodeInitializer将org.objectweb.asm.ClassVisitor接口作为超类
请帮我解决这个问题。提前谢谢
答案 0 :(得分:0)
更新你的ASM jar,你可以从这里下载,
http://grepcode.com/snapshot/repo1.maven.org/maven2/org.ow2.asm/asm-all/4.0
还要检查parboiled-core和parboiled-java jar的可扩展版本。
在lib中包含所有必需的jar后,针对新库重新编译代码并尝试运行。
答案 1 :(得分:0)
问题已解决。
实际上我的应用程序连接到mysql和mongodb。所以我一起使用hibernate和hibernate OGM。以前对于mysql我在web应用程序的.hbm.xml中写了一些命名查询。因此,在从HibernateUtil.java创建会话时使用hibernate-OGM切换到mongoDB时,hibernate扫描.hbm.xml文件,该文件包含未被Hibernate OGM解析的命名查询,因为这些查询具有sql语法。所以它引发了一个例外。在评论sql查询后,它可以正常工作。
@Zia:Thanx。