【Bug】org.hibernate.DuplicateMappingException

Hibernate 异常

背景

作者在使用IDEA整合spring、hibernate时,将对*.hbm.xml文件的扫描放到类spring的配置文件中,配置信息如下:

<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
          p:dataSource-ref="datasource"
          p:mappingLocations="classpath:com/example/pojo/*.hbm.xml"
          p:configLocation="classpath:hibernate.cfg.xml">

然后使用IDEA的generate persistence Mapping反向生成实体类及其配置文件,启动服务器后出现了如下异常信息:

Caused by: org.hibernate.DuplicateMappingException: 
Duplicate class/entity mapping com.example.pojo.AccountInfo 
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2884)

查看hibernate发现Account类被加载类两次,第二次加载时报如上异常信息

public void addClass(PersistentClass persistentClass) throws DuplicateMappingException {
            Object old = Configuration.this.classes.put(persistentClass.getEntityName(), persistentClass);
            if(old != null) {
                throw new DuplicateMappingException("class/entity", persistentClass.getEntityName());
            }

在确认刚添加的*.hbm.xml无异常后,使用搜索引擎查询该异常信息,搜到的结果都是说*.hbm.xml文件中的配置有问题,如字段名、class值等。抱着试试看的心态打开了hibernate.cfg.xml文件,发现该文件中居然配置了如下信息:

<mapping resource="com/example/pojo/AccountInfo.hbm.xml"/>
<mapping class="com.example.pojo.AccountInfo"/
解决方法:删除上述配置信息

注意:使用工具生成相关配置信息,如出现错误,可先把相关的配置文件检查一边,以防止工具帮我们往默认的配置文件中添加配置信息而导致的错误!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值