Java 休眠:未找到映射异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18185683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: Mapping not found exception
提问by Mono Jamoon
Started Hibernate from its official docs. I am stuck at the most basic step of configuring the xml files. Although I have done everything as described in the examples but it fails to find the The mapping file
.
从官方文档开始 Hibernate。我被困在配置 xml 文件的最基本步骤。虽然我已经按照示例中的描述完成了所有操作,但它无法找到 The mapping file
.
This is the exception:
这是例外:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: testing/ground/beans/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:728)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2115)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1935)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
at testing.ground.test.HibernateTest.main(HibernateTest.java:14)
The code with main():
带有 main() 的代码:
package testing.ground.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
private SessionFactory sessionFactory;
public static void main(String[] args) {
HibernateTest ht = new HibernateTest();
ht.sessionFactory = new Configuration().configure().buildSessionFactory();
ht.getData();
}
public void getData(){
Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
List list = session.createQuery("from users").list();
System.out.println(list);
session.getTransaction().commit();
session.close();
}
}
The Hibernate config file:
休眠配置文件:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/somedb?characterEncoding=UTF-8</property>
<property name="connection.username">username</property>
<property name="connection.password"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="testing/ground/beans/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I have tried to keep the folder structure same as that in the Hiberante examples:
我试图保持与 Hiberante 示例中的文件夹结构相同:
Please advice.
请指教。
采纳答案by Sagar
place user.hbm.xml file in resource folder under testing.ground.beans package
将 user.hbm.xml 文件放在 testing.ground.beans 包下的资源文件夹中
回答by Adam Dyga
Move User.hbm.xml
to proper package (testing.ground.beans
) in src/main/resources
. I guess your build tool doesn't pick the resources from src/main/java
移动User.hbm.xml
到适当的包 ( testing.ground.beans
) 中src/main/resources
。我猜你的构建工具没有从中选择资源src/main/java
回答by JB Nizet
It looks like you're using Maven to build your project. Maven expects resources (i.e. non Java files) to be under src/main/resources
. Not undr src/main/java
.
看起来您正在使用 Maven 来构建您的项目。Maven 期望资源(即非 Java 文件)在src/main/resources
. 不是src/main/java
。
That said, find another tutorial that teaches how to map entities using annotations. XML mapping is obsolete, and much harder to use than annotations. As an addd bonus, you'll also learn how to use EclipseLink and all the other JPA engines, because annotations are standard, whereas XML mapping is not.
也就是说,找到另一个教程,教如何使用注释映射实体。XML 映射已经过时,而且比注释更难使用。另外,您还将学习如何使用 EclipseLink 和所有其他 JPA 引擎,因为注释是标准的,而 XML 映射不是。
回答by kovko
You can also use
<mapping file="src/main/java/testing/ground/beans/User.hbm.xml" />
你也可以使用
<mapping file="src/main/java/testing/ground/beans/User.hbm.xml" />