我编写了一个简单的hibernate应用程序,但是当我运行时出现此错误
Initial SessionFactory creation failed: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
Exception in thread "main" java.lang.ExceptionInInitializerError
at hibernatedemopart1.Util.HibernateUtil.<clinit>(HibernateUtil.java:35)
at hibernatedemopart1.HibernateDemoPart1.main(HibernateDemoPart1.java:23)
这是我的HibernateUtil类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hibernatedemopart1.Util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
*
* @author phate
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
// private static Configuration configuration = new Configuration();
static{
try{
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}catch(Throwable ex){
System.err.println("Initial SessionFactory creation failed: " + ex);
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
我不明白错误在哪里。最初在我的HibernateUtil类中,我使用了这段代码
...
sessionFactory = new Configuration().configure().buildSessionFactory();
...
但我发现它已被弃用,所以我决定使用建议的解决方案here