muthiahmerchant
asked on
Hibernate java.lang.NoClassDefFoundError
Hi I am trying a simple Hibernate example. But I am getting a NoClassDefFoundError. initially i did not have dom4j etc in my /lib folder but at that time it used to indicate which class is not found but this time it just says class not found and hence i am lost as to which class its not able to find.
also where am i suppose to place the hibernate.cfg.xml file and how does the app know where to pick it from.
this is how I am calling hibernate.
Session session = HibernateUtil.getSessionFa ctory().ge tCurrentSe ssion();
log.info("inside 3");
session.beginTransaction() ;
log.info("inside 4");
session.save(hotelDTO);
log.info("inside 5");
session.getTransaction().c ommit();
log.info("inside 6");
HibernateUtil.getSessionFa ctory().cl ose();
also where am i suppose to place the hibernate.cfg.xml file and how does the app know where to pick it from.
this is how I am calling hibernate.
Session session = HibernateUtil.getSessionFa
log.info("inside 3");
session.beginTransaction()
log.info("inside 4");
session.save(hotelDTO);
log.info("inside 5");
session.getTransaction().c
log.info("inside 6");
HibernateUtil.getSessionFa
ASKER
java.lang.ExceptionInIniti alizerErro r
com.myapp.database.Hiberna teUtil.<cl init>(Hibe rnateUtil. java:17)
com.myapp.hotel.Hotel.inse rtHotel(Ho tel.java:8 1)
com.myapp.hotel.HotelFormA ction.exec ute(HotelF ormAction. java:57)
org.apache.struts.action.R equestProc essor.proc essActionP erform(Req uestProces sor.java:4 19)
org.apache.struts.action.R equestProc essor.proc ess(Reques tProcessor .java:224)
org.apache.struts.action.A ctionServl et.process (ActionSer vlet.java: 1196)
org.apache.struts.action.A ctionServl et.doPost( ActionServ let.java:4 32)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 709)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
org.netbeans.modules.web.m onitor.ser ver.Monito rFilter.do Filter(Mon itorFilter .java:362)
this is my HibernateUtil.java
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure( ).buildSes sionFactor y();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initia l SessionFactory creation failed." + ex);
throw new ExceptionInInitializerErro r(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
com.myapp.database.Hiberna
com.myapp.hotel.Hotel.inse
com.myapp.hotel.HotelFormA
org.apache.struts.action.R
org.apache.struts.action.R
org.apache.struts.action.A
org.apache.struts.action.A
javax.servlet.http.HttpSer
javax.servlet.http.HttpSer
org.netbeans.modules.web.m
this is my HibernateUtil.java
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initia
throw new ExceptionInInitializerErro
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
its not in the classpath. does the classpath need to be something like c:/hibernate.cfg.xml or just c:/ is enough if i have hte file in c:/
Thanks
Thanks
>>just c:/ is enough if i have hte file in c:/
Yes - C:\ should be part, or the whole of the classpath, depending on the rest of the app
Yes - C:\ should be part, or the whole of the classpath, depending on the rest of the app
ASKER
I am still getting the same error, but in a different location
java.lang.NoClassDefFoundE rror
com.myapp.hotel.Hotel.inse rtHotel(Ho tel.java:8 1)
com.myapp.hotel.HotelFormA ction.exec ute(HotelF ormAction. java:57)
org.apache.struts.action.R equestProc essor.proc essActionP erform(Req uestProces sor.java:4 19)
org.apache.struts.action.R equestProc essor.proc ess(Reques tProcessor .java:224)
this is the method in hotel.java and line 81 is Session session = HibernateUtil.getSessionFa ctory().ge tCurrentSe ssion();
public int insertHotel(HotelDTO hotelDTO) throws SQLException{
log.info("inside insertHotel");
try{
log.info("inside 1");
hotelDTO.setHotelId(DBStuf f.getPrima ryKey("hot elId","TOU R_HOTELS") );
log.info("inside 2");
Session session = HibernateUtil.getSessionFa ctory().ge tCurrentSe ssion();
log.info("inside 3");
session.beginTransaction() ;
log.info("inside 4");
session.save(hotelDTO);
log.info("inside 5");
session.getTransaction().c ommit();
log.info("inside 6");
HibernateUtil.getSessionFa ctory().cl ose();
}catch (Exception e){
e.printStackTrace();
}finally{
}
return hotelDTO.getHotelId();
}
java.lang.NoClassDefFoundE
com.myapp.hotel.Hotel.inse
com.myapp.hotel.HotelFormA
org.apache.struts.action.R
org.apache.struts.action.R
this is the method in hotel.java and line 81 is Session session = HibernateUtil.getSessionFa
public int insertHotel(HotelDTO hotelDTO) throws SQLException{
log.info("inside insertHotel");
try{
log.info("inside 1");
hotelDTO.setHotelId(DBStuf
log.info("inside 2");
Session session = HibernateUtil.getSessionFa
log.info("inside 3");
session.beginTransaction()
log.info("inside 4");
session.save(hotelDTO);
log.info("inside 5");
session.getTransaction().c
log.info("inside 6");
HibernateUtil.getSessionFa
}catch (Exception e){
e.printStackTrace();
}finally{
}
return hotelDTO.getHotelId();
}
YOu need to put your Hotel class in the classpath too
ASKER
what?? does that mean that i will have to have all the classes in the class path. does this mean that if i have the following folders then i have to specify each of them in teh classpath.
com.myapp.struts.a, com.myapp.struts.b, com.myapp.struts.c
is there a better way of doing this.
Thanks
com.myapp.struts.a, com.myapp.struts.b, com.myapp.struts.c
is there a better way of doing this.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was able to get it to run, I guess I was missing some jars. but now I don't get any errors, but I don't see the record in the database either. Also for now I hardcoded the location of hibernate.cfg.xml.
This is what my hibernate.cfg.xml looks like
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection .driver_cl ass">com.m icrosoft.j dbc.sqlser ver.SQLSer verDriver< /property>
<property name="hibernate.connection .url">jdbc :microsoft :sqlserver ://127.0.0 .1:1433;Da tabaseName =db</prope rty>
<property name="hibernate.connection .username" >sa</prope rty>
<property name="hibernate.connection .password" >xxx</prop erty>
<property name="hibernate.connection .pool_size ">10</prop erty>
<property name="show_sql">true</prop erty>
<property name="dialect">org.hiberna te.dialect .SQLServer Dialect </property>
<property name="hibernate.hbm2ddl.au to">create </property >
<!-- Mapping files -->
<mapping resource="contact.hbm.xml" />
</session-factory>
</hibernate-configuration>
and this is the code that inserts calls the save method
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
File f = new File("C:\\MyProjects\\hibe rnateTutor ial\\hiber nate.cfg.x ml");
SessionFactory sessionFactory = new Configuration().configure( f).buildSe ssionFacto ry();
session =sessionFactory.openSessio n();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Insert ing Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deep ak");
contact.setLastName("Kumar ");
contact.setEmail("deepak_3 8@yahoo.co m");
session.save(contact);
System.out.println("Done") ;
}catch(Exception e){
System.out.println(e.getMe ssage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
Thanks for all the help
This is what my hibernate.cfg.xml looks like
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection
<property name="hibernate.connection
<property name="hibernate.connection
<property name="hibernate.connection
<property name="hibernate.connection
<property name="show_sql">true</prop
<property name="dialect">org.hiberna
<property name="hibernate.hbm2ddl.au
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"
</session-factory>
</hibernate-configuration>
and this is the code that inserts calls the save method
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
File f = new File("C:\\MyProjects\\hibe
SessionFactory sessionFactory = new Configuration().configure(
session =sessionFactory.openSessio
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Insert
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deep
contact.setLastName("Kumar
contact.setEmail("deepak_3
session.save(contact);
System.out.println("Done")
}catch(Exception e){
System.out.println(e.getMe
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
Thanks for all the help
ASKER
I added a transaction start and end after save and it worked fine.
Thanks
Muthiah
Thanks
Muthiah
ASKER
can you folks suggest a good book for hibernate for beginners.
Please post full stack trace