级联Type.ALL不起作用 [英] Cascade Type.ALL not working
问题描述
我在我的实体关系中设置了CascadeType.ALL,但是当我保留一个实体时它部分起作用.
I have set CascadeType.ALL in my entity relation, but it works partially whenevr I persist an entity.
例如: `成员实体:
@OneToMany(mappedBy="member", cascade={CascadeType.ALL})
private List<ContactInfo> contactInfos;
和ContactInfo实体:
and ContactInfo entity :
@ManyToOne
@JoinColumn(name="MEMBERID")
private Member member;
`
Member
详细信息以及ContactInfo
数据都将保留.但是Member.Id
在ContactInfo
表中未更新,因为我在ContactInfo
表中具有可为空的外键约束.
Member
details and also ContactInfo
data are persisted. But Member.Id
is not updated in ContactInfo
table as I have nullable foreignkey constraint in ContactInfo
table.
每当我保持Member身份时,如何使JPA自动更新ContactInfo中的Member.Id?
How would I make JPA to automatically update Member.Id in ContactInfo also whenever I persist Member?
此致
萨蒂亚
推荐答案
如果使用CascadeType.ALL仅在ContactInfo中级联成员,则Member是拥有方.您必须删除mappedby,复制@JoinColumn信息,并将@ManyToOne放置为不可插入且不可更新.这将告诉休眠状态,在保存MEMBER时必须更新CONTACTINFO的MEMBERID.
If you use the CascadeType.ALL to only cascade the member in the ContactInfo, then the Member is the owning side. You have to remove the mappedby, duplicate the @JoinColumn info and put the @ManyToOne side as non-insertable and non-updatable. This will tell hibernate that the MEMBERID of CONTACTINFO must be updated when saving a MEMBER.
以下是映射:
成员实体:
@OneToMany
@JoinColumn(name="MEMBERID") //we need to duplicate the physical information
private List<ContactInfo> contactInfos;
联系人实体:
@ManyToOne
@JoinColumn(name="MEMBERID", insertable=false, updatable=false)
private Member member;
这篇关于级联Type.ALL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!