Hi all
i am new to hibernate i am getting Exception when i try to execute this first example through Eclipse Ide please clarify this doubt
Thanks for advance:
Exception:
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/dom4j/DocumentException
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:44)
FirstExample.java
import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Deepak Kumar
*
*
http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
/** auto generated
* @es_generated
*/
public static void main(String[] args) {
Session session=null ;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
session = HibernateUtil.currentSession();
Transaction tx = (Transaction) session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
tx.commit();
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
HibernateUtil.java
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.io.*;
public class HibernateUtil
{
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println(" Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
{
Session s=null;
try
{
// sessionFactory = new Configuration().configure().buildSessionFactory();
s = (Session) session.get();
if (s == null)
{
System.out.println("hi opening session");
s = sessionFactory.openSession();
session.set(s);
}
}catch(Exception e){e.printStackTrace();}
return s;
}
public static void closeSession(){
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
System.out.println("hi closing session");
}
}
Contact.java
public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;
/**
* @return Email
*/
public String getEmail() {
return email;
}
/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}
/**
* @return Last name
*/
public String getLastName() {
return lastName;
}
/**
* @param string Sets the Email
*/
public void setEmail(String string) {
email = string;
}
/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}
/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
/**
* @return ID Returns ID
*/
public long getId() {
return id;
}
/**
* @param l Sets the ID
*/
public void setId(long l) {
id = l;
}
}
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
contact.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">
<hibernate-mapping>
<class name="Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>