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

时间:2017-05-22 01:14:47

标签: java mysql sql hibernate persistence

  

解决

我做了什么来解决它?

我将Persistence JPA2.1添加到了库

初始错误

我有错误

Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
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/NamedStoredProcedureQuery
at org.hibernate.cfg.AnnotationBinder.bindDefaults(AnnotationBinder.java:276)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1402)
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.NamedStoredProcedureQuery
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)
... 7 more

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

主要类:

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());
       } 
     }
}

HibernateUtil 类:

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;
}
}

映射 Class.hbm.xml 文件:

 <?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>

我的 hibernate.cfg.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>

文库 enter image description here

我不知道为什么我会收到这个错误。

希望有人可以帮助我。感谢

1 个答案:

答案 0 :(得分:1)

<强>解决 我做了什么来解决它?

我将Persistence JPA2.1添加到了库

有任何问题吗?我很乐意帮忙