maven项目中 org.hibernate.MappingNotFoundException: resource:**.hbm.xml not found问题的解决方案

maven中配置hibernate,文件结构如下:

测试:

    public static void main(String[] args)
    {
        AccountEntity account = new AccountEntity();
        account.setName("kobe bryant");
        account.setMoney(9000.0);
//        1.加载主配置文件
        org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
//        默认到类的根路径下加载
        cfg.configure();
//        2.根据配置文件创建SessionFactory
        SessionFactory sessionFactory = cfg.buildSessionFactory();
//        3.创建Session
        Session session = sessionFactory.openSession();
//        4.开启事务
        Transaction transaction = session.beginTransaction();
//        5.执行操作
        session.save(account);
//        6.提交事务
        transaction.commit();
//        7.释放资源
        session.close();
        sessionFactory.close();
    }

然后报错: org.hibernate.MappingNotFoundException: resource:**.hbm.xml not found

原因:maven项目中,编译时默认只会将resources中的资源文件拷贝到target文件夹中。我的**.hbm.xml文件是在domain中,当然找不到了。

解决方案:在pom.xml中配置资源文件

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/AccountEntity.hbm.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/SysResourceEntity.hbm.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

解决!

猜你喜欢

转载自blog.csdn.net/qq_22339269/article/details/83017903