gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:48:51 29 4
gpt4 key购买 nike

SOLVED

我做了什么来解决这个问题?

我将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

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

希望有人能帮助我。谢谢

最佳答案

已解决我做了什么来解决这个问题?

我将Persistence JPA2.1添加到库

有什么问题吗?我很乐意提供帮助

关于java - 初始 SessionFactory 创建失败 : java. lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44103346/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com