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

Community
  • 1
  • 1
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • DO you have the class in your classpath ? – ArunM Mar 24 '14 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 ? – ArunM Mar 24 '14 at 18:03
  • @ArunM I did added hibernate jar to my project build path, please check the modification I made to my post – Renaud is Not Bill Gates Mar 24 '14 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. – ArunM Mar 24 '14 at 18:29
  • Do check in your lib. Jar required at run time. – Chowdappa Mar 24 '14 at 18:30
  • I guess u are missing hibernate jar..... – Balaji Reddy Mar 24 '14 at 18:31
  • @ArunM Okey thanks, I solved that problem, it was because I added Hibernate libraries in the Java Build Path instead of adding them into the lib folder. – Renaud is Not Bill Gates Mar 24 '14 at 18:56
  • your solution might be here [https://stackoverflow.com/questions/18188542/java-lang-classnotfoundexception-org-hibernate-hibernateexception](https://stackoverflow.com/questions/18188542/java-lang-classnotfoundexception-org-hibernate-hibernateexception) – sam Oct 20 '17 at 21:14

1 Answers1

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/

Paul
  • 580
  • 1
  • 7
  • 22