为什么可以';t Hibernate find/Hibernate.cfg.xml?
我正在努力学习Hibernate+Maven。我正在尝试连接到PostgreSQL数据库,但出现以下错误: 初始SessionFactory创建失败。org.hibernate.hibernateeException:未找到hibernate.cfg.xml为什么可以';t Hibernate find/Hibernate.cfg.xml?,hibernate,postgresql,maven,classpath,hibernate.cfg.xml,Hibernate,Postgresql,Maven,Classpath,Hibernate.cfg.xml,我正在努力学习Hibernate+Maven。我正在尝试连接到PostgreSQL数据库,但出现以下错误: 初始SessionFactory创建失败。org.hibernate.hibernateeException:未找到hibernate.cfg.xml hibernate.cfg.xml文件已经在src/main/resources文件夹下,我已经将该文件夹添加为源文件夹 以下是会话工厂初始化的代码: Configuration configuration = new Configura
hibernate.cfg.xml
文件已经在src/main/resources
文件夹下,我已经将该文件夹添加为源文件夹
以下是会话工厂初始化的代码:
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
return configuration.buildSessionFactory();
编辑:这是pom.xml文件
<modelVersion>4.0.0</modelVersion>
<groupId>com.jee.mavenapp</groupId>
<artifactId>hibernate-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hibernate-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
4.0.0
com.jee.mavenapp
休眠示例
0.0.1-快照
罐子
休眠示例
http://maven.apache.org
UTF-8
朱尼特
朱尼特
3.8.1
测试
org.hibernate
冬眠核心
4.3.10.最终版本
org.hibernate.common
hibernate commons注释
4.0.5.4最终版本
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.0.1决赛
org.slf4j
slf4j api
1.7.12
在xml文件前面添加正斜杠
Configuration configuration = new Configuration().configure("/hibernate.cfg.xml");
您也可以省略参数,因为它是
这是加载您计算机上的文件所需的操作。在xml文件前面添加正斜杠
Configuration configuration = new Configuration().configure("/hibernate.cfg.xml");
您也可以省略参数,因为它是
这是加载服务器上的文件所需的操作。这不是问题的解决方案,但是您可以将hibernate.cfg.xml文件与构建会话工厂的Java类放在同一个包中(以确保它位于类路径中)。然后像这样做
Configuration configuration = new Configuration().configure("/com/github/fluent/hibernate/example/mysql/hibernate.cfg.xml");
return configuration.buildSessionFactory();
用类的包替换“/com/github/fluent/hibernate/example/mysql/”
另外,您可以在这里找到一个非常简单的控制台Hibernate应用程序这不是问题的解决方案,但是您可以将Hibernate.cfg.xml文件与构建会话工厂的Java类放在同一个包中(以确保它位于类路径中)。然后像这样做
Configuration configuration = new Configuration().configure("/com/github/fluent/hibernate/example/mysql/hibernate.cfg.xml");
return configuration.buildSessionFactory();
用类的包替换“/com/github/fluent/hibernate/example/mysql/”
另外,您可以在这里找到一个非常简单的控制台Hibernate应用程序谢谢您的回复。我已经测试了这些选项,但仍然得到相同的错误。还有其他建议吗?@TuncerTunçer
/src/main/resources
应该是资源文件夹,而不是源文件夹。你能包括相关的pom片段吗?谢谢你的回复。我已经测试了这些选项,但仍然得到相同的错误。还有其他建议吗?@TuncerTunçer/src/main/resources
应该是资源文件夹,而不是源文件夹。你能包括相关的pom片段吗?