9

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

Satya
  • 2,094
  • 6
  • 37
  • 60
  • Whatever I try, nothing works. Any suggestion would be of great help. I use Hibernate implementation and do not use hibernate session for persisting, so hopefully CascadeType.ALL should work. – Satya Feb 26 '11 at 18:57
  • Perhaps you could post your Member.Id declaration along with the annotation so we could have a better picture of your problem ? And also, please explain why you need to update the Id field ? Is the Id field the primary key of Member entity ? – Bertie Mar 22 '11 at 17:02

1 Answers1

4

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

Simon LG
  • 2,907
  • 1
  • 17
  • 16
  • 3
    Almost after a year...Yes I will surely try this whenever I find the time. Thanks a lot for the update and link :) – Satya Feb 06 '12 at 03:55