[英]How to fix mapping not found exception for Hibernate 5?
您好,我是休眠的新手,目前我正在尝试做一些练习。 但是有一些错误。
我尝试在我的项目中使用休眠模式。 我阅读了教程,并开始将Hibernate集成到我的java maven项目中。 首先,我添加了下一个依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
下一步是将hibernate.cfg.xml文件添加到src / main / resources目录中。 该文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/testprojectdatabase</property>
<property name="hibernate.connection.username">username_here</property>
<property name="hibernate.connection.password">password_here</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<mapping resource="milkiv/mytestproject/models/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
然后创建用于映射user_info
表的UserInfo.java
并将其放在src/main/java/milkiv/mytestproject/models/UserInfo.java
,并将UserInfo.hbm.xml
文件放入src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml
。
我还创建了类UserInfoManager
,如下所示:
public class UserInfoManager implements ManagerAdd<UserInfo> {
private final SessionFactory factory;
public UserInfoManager() {
factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
}
public int add(UserInfo user) {
Transaction transaction = null;
Integer userId = null;
try (Session session = factory.openSession()){
transaction = session.beginTransaction();
userId = (Integer) session.save(user);
transaction.commit();
} catch (HibernateException he) {
if (transaction != null) {
transaction.rollback();
}
}
return userId;
}
}
当我尝试为方法add(UserInfo user)
创建测试时,由于工厂初始化,它在UserInfoManager
构造函数上失败,原因是
Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml)
所以最终我知道了哪里,但不知道如何解决。 我已经阅读了很多有关stackoverflow的问题的答案,但是没有人帮我解决此错误。
我将不胜感激任何想法,帮助和解释如何解决此问题...
完整堆栈跟踪:找不到映射(RESOURCE):milkiv / mytestproject / models / UserInfo.hbm.xml:origin(milkiv / mytestproject / models / UserInfo.hbm.xml)org.hibernate.boot.MappingNotFoundException at org.hibernate.boot org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)上的.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)在org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70 )的org.hibernate.cfg.Configuration.buildSessionFactory()在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)的org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)处的Configuration.java:692)milkiv.mytestproject.managers.UserInfoManager。(UserInfoManager.java:22)处milkiv.mytestproject.managers.UserInfoManagerTest.testAdd( UserInfoManagerTest.java:24),位于sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method),位于s org.junit上的java.lang.reflect.Method.invoke(Method.java:497)上的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)上的un.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)处的.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)在org.junit.runners.model.FrameworkMethod.invokeExplosively( org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)处的FrameworkMethod.java:41)org.junit.runners处的org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)上的org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)上的。 .runners.ParentRunner $ 1.schedule(ParentRunner.java:52)在org.junit.runners.ParentRunner.runChildren( 在org.junit.runners.ParentRunner.access处$ 000(ParentRunner.java:42)在org.junit.runners.ParentRunner处$ 2.evaluate(ParentRunner.java:184)在org.junit.runners.ParentRunner处的ParentRunner.java:191) org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)的.run(ParentRunner.java:236)在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141 )于org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)于sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)处org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:处java.lang.reflect.Method.invoke(Method.java:497)处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 189)在org.apache.maven.surefire.booter.Provi(ProviderFactory.java:165)在org.apache.maven.surefire.booter.Provi org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java :)上的derFactory.invokeProvider(ProviderFactory.java:85)在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 75)
更新 如果有人需要,可以在@ v.ladynev答案的注释中解决。
我检查了您的方法,一切正常。
将UserInfo.hbm.xml
放在src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml
它会出现在
bin/milkiv/mytestproject/models/UserInfo.hbm.xml
构建后(而不是bin
可以是您的构建文件夹)。
因此, /milkiv/mytestproject/models/
看起来像build文件夹中的其他源包。 请检查一下。 检查类路径中是否有resource
文件夹。
尝试在开头添加/
<mapping resource="/milkiv/mytestproject/models/UserInfo.hbm.xml"/>
更新
在开头添加/
没关系,因为Hibernate会在ClassLoader
之前将其删除。 因此,最有效的方法-开头没有/
。
尝试将UserInfo.hbm.xml
放在resources
的根目录中
<mapping resource="UserInfo.hbm.xml"/>
更新
尝试测试
public UserInfoManager() {
System.out.println(UserInfoManager.class
.getResource("/milkiv/mytestproject/models/UserInfo.hbm.xml"));
System.out.println(ClassLoader.getSystemClassLoader().getResource(
"milkiv/mytestproject/models/UserInfo.hbm.xml"));
}
注意前导/
。 控制台输出什么
new UserInfoManager();
@NickolasUA您正在使用Hibernate5。因此更改hibernate.cfg.xml中的最后一行
<mapping class="milkiv.mytestproject.models.UserInfo.hbm.xml"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.