如何修复Hibernate 5的映射未找到异常?

时间:2016-03-02 01:24:45

标签: java hibernate maven configuration hbmxml

你好我是hibernate的新手,现在我想在实践中做点什么。但是有一些错误。

我尝试在我的项目中使用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.javaUserInfo.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     在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)     at milkiv.mytestproject.managers.UserInfoManagerTest.testAdd(UserInfoManagerTest.java:24)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at 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)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)     at 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回答的评论中解决。

2 个答案:

答案 0 :(得分:2)

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

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();

答案 1 :(得分:1)

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

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