3

I've created a web dynamic project, and want to use Hibernate with it.

Then, I used the Hibernate Code Generation to generate classes code, and I created a class HibernateUtil where I initialize SessionFactory.

public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory(){ try{ SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); return sessionFactory; }catch(Exception ex){ ex.printStackTrace(); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { getSessionFactory().close(); } } 

And another class HibernateSessionFactoryListener :

public class HibernateSessionFactoryListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent arg0) { Session session = HibernateUtil.getSessionFactory().openSession(); System.out.println("\n Context Initilaise \n"); } @Override public void contextDestroyed(ServletContextEvent arg0) { HibernateUtil.shutdown(); System.out.println("\n Context Detruit \n"); } } 

When I run my project I get this error message :

java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration at com.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:28) at com.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:11) at com.hibernate.HibernateSessionFactoryListener.contextInitialized(HibernateSessionFactoryListener.java:12) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569) ... 12 more 

How can I solve this problem ?

Edit 1 :

This is my Java Build Path :

enter image description here

8
  • DO you have the class in your classpath ? Commented Mar 24, 2014 at 17:16
  • PLease add the hibernate jar onto your class path .. That should solve the problem. Which is the version of hibernate you are using ? Commented Mar 24, 2014 at 18:03
  • @ArunM I did added hibernate jar to my project build path, please check the modification I made to my post Commented Mar 24, 2014 at 18:10
  • 1
    I think the problem is that the hibernate JARS are not part of your web app libraries. Please check you WAR to see if the hibernate core jar is available in WEB-INF/lib. Commented Mar 24, 2014 at 18:29
  • Do check in your lib. Jar required at run time. Commented Mar 24, 2014 at 18:30

1 Answer 1

11

In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“. So , you can safely replace your “AnnotationConfiguration” with “Configuration” class

Source: http://www.mkyong.com/hibernate/hibernate-the-type-annotationconfiguration-is-deprecated/

Sign up to request clarification or add additional context in comments.

1 Comment

I changed it in my -servlet.xml file. Got rid of that error!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.