你好,我是hibernate的新手,目前我正在尝试在实践中做些事情。但有一些错误。如何修复Hibernate 5没有发现异常的映射?
我尝试在我的项目中使用hibernate。我阅读了教程,并开始将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)
所以最终我知道在哪里,但不知道如何解决这个问题。我已经阅读了很多关于这个问题的答案,但没有人不帮我解决这个错误。
我欢迎任何想法,帮助和解释如何解决这个问题...
完整的堆栈跟踪: 映射(资源)未发现:milkiv /为MyTestProject /模型/ UserInfo.hbm.xml :origin(milkiv/mytestproject/models/UserInfo.hbm.xml) org.hibernate.boot.MappingNotFoundException at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56) at org.hibernate.boot .MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) 在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) 在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在org.hibernate.cfg.Configuration。 buildSessionFactory(Configuration.java:692) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at milkiv.mytestproject.managers.UserInfoManager。(UserInfoManager.java:22) at milkiv.mytestproject.managers .UserInfoManagerTest.testAdd(UserInfoManagerTest.java:24) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.jav一:43) 在java.lang.reflect.Method.invoke(Method.java:497) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44) 在org.junit.internal .runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:20) ,位于org.junit.runners.BlockJUnit4ClassRunner。runNotIgnored(BlockJUnit4ClassRunner.java:79) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 在org.junit。 Runner.ParentRunner $ 3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42) at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run( ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at sun.reflect。 NativeMethodAccessorImpl.invoke0(本机方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke (Method.java:497) 在org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在org.apache.maven.surefire.booter.ProviderFactory $ ProviderProxy.invoke(ProviderFactory.java :165) at org.apache.maven.surefire.booter.ProviderFactory.inv okeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java: 75)
UPDATE 如果任何人有需要,解决在v.ladynev答案@的意见。
请,下一次加满堆栈跟踪。 –