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主键作为外键必然出错。
千万不要采取提示的的在多方设置insert=false,update=false,如果一方设置了inverse=true,那么从表的外键就是空值;
如果设置了inverse=false,那么就会多出两条update语句,两种方法均不合适,况且问题也不是出在这里
应该把<column
name="petsId"/>改成<column
name="pets_id"/>外键
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"/>外键
相关文章推荐
- org.hibernate.MappingException: Repeated column in mapping for entity
- Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.yyf.entity.Cus
- caused by org.hibernate.mappingexception repeated column in mapping for entity
- Hibernate中的"Repeated column in mapping for entity"异常
- org.hibernate.MappingException: Repeated column in mapping for entity
- Repeated column in mapping for entity:(should be mapped with insert="false" update="false")
- Repeated column in mapping for entity
- Repeated column in mapping for entity
- Repeated column in mapping for entity: cn.cnic.qlms.entity.User column: id (should be mapped with in
- Repeated column in mapping for entity:should be mapped with insert="false" update="false"
- 玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert="false" update="fal
- Repeated column in mapping for entity: .... should be mapped with insert="false" update="false"
- Repeated column in mapping for entity:(should be mapped with insert="false" update="false")
- Repeated column in mapping for entity@Column(name = "type_id") private Long typeId = 0L; // //
- 玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert="false" update="fal
- Repeated column in mapping for entity:should be mapped with insert="false" update="false"
- Repeated column in mapping for entity: XXpojo column: xx (should be mapped with insert="false" upda
- Repeated column in mapping for entity
- 玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert="false" update="fal
- 实体添加重复列错误(Repeated column in mapping for entity)