我是noobie在这里,但我一直在找到我的问题的解决方案在这里,我没有看到它...我是新的hibernate和我利用我的空闲时间来学习这个。
package negocio;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import integracion.HibernateUtil;
import integracion.dao.BodegaDAOImpl;
import integracion.dao.VinoDAO;
import integracion.dao.VinoDAOImpl;
import integracion.entidades.Bodega;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
//The problem is here
**sessionFactory = configuration.buildSessionFactory(serviceRegistry);**
Session session = sessionFactory.openSession();
BodegaDAOImpl bodegaDao = new BodegaDAOImpl<>();
bodegaDao.get(1);
}
}
这是添加1
的jar列表这是映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/vinoteca</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="integracion.entidades.Vino"/>
<mapping class="integracion.entidades.Bodega"/>
<mapping class="integracion.entidades.Denominacion"/>
</session-factory>
</hibernate-configuration>
dic 15, 2016 12:37:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.9.Final}
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
dic 15, 2016 12:37:51 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vinoteca]
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Thu Dec 15 00:37:51 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 15, 2016 12:37:51 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
at org.hibernate.cfg.AnnotationBinder.bindDefaults(AnnotationBinder.java:259)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:184)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.prepare(MetadataBuildingProcess.java:156)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:253)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at negocio.Main.main(Main.java:31)
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)
... 9 more
所以..这就是问题所在。当eclipse说java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
是因为没有添加jar(javax.persistence.jar或hibernate-jpa-2.1-api-1.0.0 ... jar),但我添加了它们,我不解决这个问题。
谁能说我为什么工作不好?谢谢! PD:对不起我的英语,我不是母语为英语的人=)答案 0 :(得分:0)
使用maven或gradle等构建系统来构建项目,并添加对hibernate的依赖。这几乎肯定会解决所有与基本依赖相关的类路径问题。
答案 1 :(得分:0)
将所需的jar文件添加到类路径中。