-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
PostPosted: Mon Aug 27, 2007 7:37 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
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>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 8:35 am 
Newbie

Joined: Tue Dec 05, 2006 7:40 am
Posts: 14
you need put the jars from hibernate library directory in your classpath.

sample: http://www.hibernate.org/hib_docs/refer ... orial.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 8:47 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
ualex wrote:
you need put the jars from hibernate library directory in your classpath.

sample: http://www.hibernate.org/hib_docs/refer ... orial.html


Thanks For Reply

sir

I have added all the jar files under lib folder but it is giving the same exeption

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)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 10:17 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
You must add the dom4j.jar in your project classpath in Eclipse

Right click on the project,
select Properties,
click on "Java build path" -> Libraries -> add JARs


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 12:59 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
ah wrote:
You must add the dom4j.jar in your project classpath in Eclipse

Right click on the project,
select Properties,
click on "Java build path" -> Libraries -> add JARs


Still I am getting the same Exception.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 2:40 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
ah wrote:
You must add the dom4j.jar in your project classpath in Eclipse

Right click on the project,
select Properties,
click on "Java build path" -> Libraries -> add JARs


Thanks For Reply

sir

I have added all the jar files under lib folder but it is giving the same exeption

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)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.