Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade Type.ALL not working

Tags:

jpa-2.0

hsqldb

I have set CascadeType.ALL in my entity relation, but it works partially whenevr I persist an entity.

Ex : ` Member entity :

@OneToMany(mappedBy="member", cascade={CascadeType.ALL})
private List<ContactInfo> contactInfos;

and ContactInfo entity :

@ManyToOne
@JoinColumn(name="MEMBERID")
private Member member;

`

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.

How would I make JPA to automatically update Member.Id in ContactInfo also whenever I persist Member?

Regards,

Satya

like image 717
Satya Avatar asked Feb 24 '11 16:02

Satya


1 Answers

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.

Here is the mapping:

Member entity :

@OneToMany
@JoinColumn(name="MEMBERID") //we need to duplicate the physical information
private List<ContactInfo> contactInfos;

Contact entity :

@ManyToOne
@JoinColumn(name="MEMBERID", insertable=false, updatable=false)
private Member member;

Reference Hibernate Section 2.2.5.3.1.1

like image 98
Simon LG Avatar answered Sep 20 '22 06:09

Simon LG