org.hibernate.internal.util.config.ConfigurationException:找不到cfg.xml资源
我正在尝试使用pgadmin3连接到ubuntu中的Postgresql9.1。 我的Pgadmin3 GUI工具没有通过右键单击数据库来提供创建表的任何选项,但在我看到的一些video中可以使用它。 因此,我使用终端创建数据库,它出现在pgadmin3中。
我的文件结构
我的Userdetails文件
package org.nitish.hiber; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class UserDetails { @Id private int userId; private String userName; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
我的HibernateCaller文件
package org.nitish.caller; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.nitish.hiber.UserDetails; public class HibernateTest { public static void main(String[] args) { UserDetails user = new UserDetails(); user.setUserId(1); user.setUserName("First User"); try { SessionFactory sessionFactory = new Configuration().configure("/HibernateTest/src/hibernate.cfg.xml").buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); } catch(HibernateException e) { e.printStackTrace(); } } }
的hibernate.cfg.xml
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or . --> org.postgresql.Driver jdbc:postgresql://localhost:5432/hiber nitish 1 org.hibernate.dialect.PostgreSQLDialect thread org.hibernate.cache.internal.NoCacheProvider true create
我收到了以下错误
Mar 1, 2016 9:02:48 AM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Mar 1, 2016 9:02:48 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml] at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53) at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163) at org.hibernate.cfg.Configuration.configure(Configuration.java:259) at org.nitish.caller.HibernateTest.main(HibernateTest.java:17)
做了一些更改后(在finally块中无法使用session.close()我认为不应该导致此错误)
Mar 1, 2016 10:13:34 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.1.0.Final} Mar 1, 2016 10:13:34 AM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Mar 1, 2016 10:13:34 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Mar 1, 2016 10:13:34 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. Mar 1, 2016 10:13:35 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Mar 1, 2016 10:13:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 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 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at org.nitish.caller.HibernateTest.main(HibernateTest.java:17) Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:161) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:117) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:73) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) ... 14 more Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217) at java.lang.ClassLoader.loadClass(ClassLoader.java:323) at java.lang.ClassLoader.loadClass(ClassLoader.java:268) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:274) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226) ... 25 more
如果您在源文件夹的根目录中有hibernate.cfg.xml
,那就行了
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
如果它在包中,对于org.nitish.caller
中的org.nitish.caller
,请通过这种方式指定path
SessionFactory sessionFactory = new Configuration() .configure("/org/nitish/caller/hibernate.cfg.xml").buildSessionFactory();
您需要关闭session
(在finally
块中)。 不要忘记添加rollback
代码。
请将@Table
注释添加到UserDetails
。
更新
Hibernate无法找到org.postgresql.Driver
类的错误的原因。 它驻留在postgresql jar中。 你的图像上有那个jar,但可能是你没有将它添加到类路径中。 请参阅如何在Eclipse(Java)中将JAR添加到项目构建路径 。
要关闭finally
块中的session
,您需要在try
块之外设置session
变量。
Session session = sessionFactory.openSession(); try{ } finally { session.close(); }
new Configuration().configure()
从类路径目录的根目录获取hibernate.cfg.xml
。 new Configuration().configure("/com/company/project/hibernate.cfg.xml")
取自类路径+ com / company / project / hibernate.cfg.xml的根目录 。
如果从类路径的根目录使用不同的文件名进行hibernate配置,那么例如: new Configuration().configure("/database.cfg.xml")
如果要提供配置文件的完整系统路径,则new Configuration().configure(new File("/home/visruth/config/hibernate.cfg.xml))
从给定的确切位置获取配置文件。
我通过将配置文件移动到src/mail/resources
来修复此问题。 对我来说,这是根目录。
以前的所有答案都很完美。 但是,如果你需要一个非常快速的解决方案,那么我建议你只需将你的hibernate.cfg.xml文件放在你的源文件夹中然后写
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
这很简单,有效!
new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory());
这解决了我的错误!
- Spring + Hibernate + DB2 + JTA + XA应用程序中的死锁
- 如何从不同的位置加载hibernate.cfg.xml
- Hibernate:尝试获取锁定时发现死锁
- JPA / Hibernate使用共享主键进行单向一对一映射
- Hibernate @LazyCollection注释的用途是什么?
- Hibernate 4使用java.time.LocalDate和DATE()构造
- FetchMode连接对spring JPA存储库中的ManyToMany关系没有任何区别
- 引起:java.lang.IllegalStateException:找到不明确的映射。 无法映射’appController’bean方法
- @BatchSize但在@ManyToOne案例中有很多往返