【问题标题】:How to fix mapping not found exception for Hibernate 5?如何修复 Hibernate 5 的映射未找到异常?
【发布时间】:2016-03-02 01:24:44
【问题描述】:

您好,我是 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>

然后我创建UserInfo.java 用于映射user_info 表并将其放入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) 创建测试时,由于

Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml)

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

我会感谢任何想法、帮助和解释如何解决这个问题...

完整的堆栈跟踪: 未找到映射(资源):milkiv/mytestproject/models/UserInfo.hbm.xml:起源(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(Native Method) 在 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(Native Method) 在 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)

更新 如果有人需要,请在 cmets 中为@v.ladynev 解答。

【问题讨论】:

  • 请,下次添加完整的堆栈跟踪。

标签: java hibernate maven configuration hbmxml


【解决方案1】:

我检查了您的方法,一切正常。

当您将UserInfo.hbm.xml 放入src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml

它会出现在

bin/milkiv/mytestproject/models/UserInfo.hbm.xml

构建后(而不是 bin 可以是您的构建文件夹)。

所以/milkiv/mytestproject/models/ 看起来就像构建文件夹中的其他源包。请检查一下。检查类路径中是否有 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 你检查了构建文件夹中的UserInfo.hbm.xml 吗?
  • 这是maven项目。所以最终它出现在 target\classes\resources\milkiv\mytestproject\models
  • @NickolasUA 在构建 jar 或战争之后,/milkiv/mytestproject/models/UserInfo.hbm.xml 所在的位置?它应该位于已编译源文件夹 (classes) 的根目录中,没有任何 resources。尝试指定resources/milkiv/mytestproject/models/UserInfo.hbm.xml
  • 在这两种情况下都为空。这意味着什么?
  • @NickolasUA Hibernate 使用ClassLoader 加载hbm.xml — 第二行。对resources\milkiv\mytestproject\models\UserInfo.hbm.xml 进行相同的尝试。当您获得有效路径时,Hibernate 也可以正常工作。
【解决方案2】:

@NickolasUA 你正在使用 Hibernate 5。 所以修改hibernate.cfg.xml中的最后一行

<mapping class="milkiv.mytestproject.models.UserInfo.hbm.xml"/>

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多