您的位置:首页 > 移动开发

hibernate文件配置出现Repeated column in mapping for entity的Mapping异常处理

2017-04-03 15:30 387 查看
今天在配置一对多关系的时候出现了以下异常:

org.hibernate.MappingException: Repeated column in mapping for entitycom.lrq.entity.Pets column: petsId (should be mapped with insert="false" update="false")

主要原因是mapping配置问题:

当多表关联的时候,特别是一对多关联的时候:

在一方inverse=true(一般默认是false)是将关系维护交给多方,如果不设置那么一方和多方均会进行insert或者update的sql语句,也就是说插入的时候都会插入或更新外键;而这里将petsId主键作为外键必然出错。

<many-to-one name="owners" class="com.lrq.entity.Owners">
<column name="petsId"/>
</many-to-one>
<many-to-one name="types" class="com.lrq.entity.Types">
<column name="types_id"/>
</many-to-one>
<set name="visitsSet" inverse="true" cascade="all">
<key>
<column name="pets_id"/>
</key>
<one-to-many not-found="ignore" class="com.lrq.entity.Visits"/>
</set>


<!--inverse:将关联关系的维护交给多方,在保存的时候可以减少update语句的生成,提高Hibernate的执行效率-->
<!--cascade:指的是级联操作,比如当我们保存一方数据的时候,可以自动将关联到的多方数据也插入到数据库当中-->
<set name="petsSet" inverse="true" cascade="all">
<key>
<column name="petsId"/>
</key>
<one-to-many not-found="ignore" class="com.lrq.entity.Pets"/>
</set>


千万不要采取提示的的在多方设置insert=false,update=false,如果一方设置了inverse=true,那么从表的外键就是空值;

如果设置了inverse=false,那么就会多出两条update语句,两种方法均不合适,况且问题也不是出在这里

应该把<column
name="petsId"/>改成<column
name="pets_id"/>外键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐