gpt4 book ai didi

java - 如何修复Hibernate 5找不到映射的异常?

转载 作者:行者123 更新时间:2023-12-01 21:47:48 25 4
gpt4 key购买 nike

您好,我是休眠的新手,此刻我正在尝试实践。但是有一些错误。

我尝试在我的项目中使用休眠模式。我阅读了教程,并开始将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>


然后,我创建用于映射 UserInfo.java表的 user_info并将其放在 src/main/java/milkiv/mytestproject/models/UserInfo.java中,并将其放入 UserInfo.hbm.xml文件中。

我还创建了一个类 src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml,它类似于:

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;
}
}


当我尝试为方法 UserInfoManager创建测试时,由于以下原因,它在工厂初始化时在 add(UserInfo user)构造函数上失败了:

UserInfoManager

所以最终我知道了哪里,但不知道如何解决。我已经阅读了很多关于stackoverflow的问题的答案,但是没有人没有帮助我解决此错误。

我将不胜感激任何想法,帮助和解释如何解决此问题...

全栈跟踪:
找不到映射(RESOURCE):milkiv / mytestproject / models / UserInfo.hbm.xml:origin(milkiv / mytestproject / models / UserInfo.hbm.xml)
org.hibernate.boot.MappingNotFoundException
    在org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
    在org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
    在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)
    在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    在milkiv.mytestproject.managers.UserInfoManager。(UserInfoManager.java:22)
    在milkiv.mytestproject.managers.UserInfoManagerTest.testAdd(UserInfoManagerTest.java:24)
    在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.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)
    在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    在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.runners.ParentRunner $ 3.run(ParentRunner.java:193)
    在org.junit.runners.ParentRunner上$ 1.schedule(ParentRunner.java:52)
    在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)
    在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:184)
    在org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    在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)
    在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)
    在org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java: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();

关于java - 如何修复Hibernate 5找不到映射的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35736780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com