This content has been marked as final.
Show 4 replies
-
1. Re: ManyToMany relationship removal example
epbernard Jul 26, 2006 6:14 PM (in response to scotttam)you probably remove it from the non managed side 'ie the mappedBy side' only
-
2. Re: ManyToMany relationship removal example
scotttam Jul 26, 2006 7:55 PM (in response to scotttam)Below are my two entities, Campaign and AdSlot with the relationship stored in campaign_adslot_xref. (all the other info has been removed for brevity)
One of the older positings mentioned something about ensuring that both equals and hashCode are implemented. I am not implementing those methods, could that be it?@Entity @Table(name="campaign") public class Campaign implements Serializable { private Set<AdSlot> adSlots; @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy="campaigns") public Set<AdSlot> getAdSlots() { return adSlots; } public void setAdSlots(Set<AdSlot> adSlots) { this.adSlots = adSlots; } } @Entity @Table(name="ad_slot") public class AdSlot implements Serializable { private Set<ParentCampaign> campaigns; @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY) @JoinTable(name = "campaign_adslot_xref", joinColumns = { @JoinColumn(name = "ad_slot_id") }, inverseJoinColumns = { @JoinColumn(name = "campaign_id") } ) public Set<ParentCampaign> getCampaigns() { return campaigns; } public void setCampaigns(Set<ParentCampaign> campaigns) { this.campaigns = campaigns; } }
And here is the code in my backing bean where I try to remove all the associations from this particular campaign:Set<AdSlot>adslots = currentCampaign.getAdSlots(); if (null != adslots && adslots.size() > 0) { for (AdSlot as : adslots) { as.getCampaigns().remove(currentCampaign); } } currentCampaign.getAdSlots().clear(); Set<Ad> ads = currentCampaign.getAds(); if (null != ads && ads.size() > 0) { for (Ad a : ads) { a.getAdSlots().clear(); } }
-
3. Re: ManyToMany relationship removal example
epbernard Jul 28, 2006 2:40 PM (in response to scotttam)are you using those object in the detached state? Then yes you need to implement a proper equals/hashcode impl
-
4. Re: ManyToMany relationship removal example
martinluene Sep 1, 2006 10:41 AM (in response to scotttam)cascade=REMOVE should not be applied to ManyToMany relations,
perhaps you want to change cascade=ALL to a lower level.