初始SessionFactory创建失败:java.lang.NoClassDefFoundError:javax / persistence / EntityListeners

时间:2017-05-21 23:56:49

标签: java mysql sql hibernate libraries

解决

  

我将Initial SessionFactory creation failed: java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery 添加到了库

内心错误已解决,但现在我另一个

Initial SessionFactory creation failed: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
Exception in thread "main" java.lang.ExceptionInInitializerError
at modelo.util.HibernateUtil.<clinit>(HibernateUtil.java:29)
at modelo.dao.GenericDAO.getAll(GenericDAO.java:89)
at principal.Main.main(Main.java:34)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:100)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:252)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1386)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at modelo.util.HibernateUtil.<clinit>(HibernateUtil.java:25)
... 2 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more

有什么想法吗?

  

Inicial post

     

我有错误

public class Main {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {
       GenericDAO clienteDAO = new GenericDAO();
       List<Cliente> list = clienteDAO.getAll(Cliente.class);
       System.out.println(list.size());
       for(Cliente c : list)
         {System.out.println("-> " + c.getNombre());
       } 
     }
}

不知道为什么我会得到那个例外

主要类:

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

HibernateUtil 类:

 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <!-- Generated 21/05/2017 17:46:05 by Hibernate Tools 4.3.1 -->
 <hibernate-mapping>
<class name="modelo.pojo.Cliente" table="cliente" catalog="basededatosprueba" optimistic-lock="version">
    <id name="codigo" type="java.lang.Integer">
        <column name="codigo" />
        <generator class="identity" />
    </id>
    <property name="nombre" type="string">
        <column name="nombre" />
    </property>
    <property name="direccion" type="string">
        <column name="direccion" />
    </property>
    <property name="telefono" type="string">
        <column name="telefono" />
    </property>
    <property name="cuit" type="string">
        <column name="cuit" />
    </property>
    <property name="cp" type="java.lang.Integer">
        <column name="cp" />
    </property>
    <property name="saldo" type="java.lang.Double">
        <column name="saldo" precision="22" scale="0" />
    </property>
    <property name="deuda" type="java.lang.Double">
        <column name="deuda" precision="22" scale="0" />
    </property>
</class>
</hibernate-mapping>

映射 Class.hbm.xml 文件:

 <hibernate-configuration>
 <session-factory>
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/basededatosprueba?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<mapping resource="modelo/pojo/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我的 hibernate.cfg.xml

hibernate-jpamodelgen-1.2.0.Final.jar

(我还添加了从评论中下载的hibernate-jpa-2.0-api-1.0.1.Final.jar)

libraries

我不知道为什么我会收到这个错误。 如果你问我,我已经添加了drop 1 [] -- result: [] 。 希望可以有人帮帮我。感谢

2 个答案:

答案 0 :(得分:1)

点击以下链接下载hibernate-jpa-2.0-api-1.0.1.Final.jar并将其放入项目库中,希望您的应用程序能正常运行。

http://repo1.maven.org/maven2/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar

答案 1 :(得分:0)

我遇到了同样的问题。在我的情况下, hibernate-jpa-2.1-api-1.0.0.Final.jar 解决了这个问题,以及hibernate-core.jar,hibernate-validator.jar,hibernate-commons-annotations.jar和hibernate-entitymanager.jar