I am try to configure my spring web app with Hibernate OGM for mongoDB with java 1.6 and OGM 4.1.3.Final.
For Session I write HibernateUtil.java which is working fine with JUnit test but It throws Exception.
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
dependency used for OGM are
<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>
After deployment on Tomcat I am hitting through another jsp that time HibernateUtil.java throws an Exception at following Line
OgmConfiguration cfgogm = new OgmConfiguration();
Exception Line is
java.lang.IncompatibleClassChangeError: class org.parboiled.transform.ClassNodeInitializer has interface org.objectweb.asm.ClassVisitor as super class
Please Help me to resolve this. Thank You in advance
Issue resolved.
Actually my application is connected to both mysql and mongodb. so i am using hibernate and hibernate OGM together. Previously for mysql I wrote some named queries in .hbm.xml in web app. So while switching to mongoDB with hibernate-OGM at the time of session creation from HibernateUtil.java hibernate scans .hbm.xml file which is containes named queries which is not parsed by Hibernate OGM because those queries have sql syntax. So it throws an exception. After commenting sql queries it works fine.
@Zia : Thanx.