4 Replies Latest reply on Sep 1, 2006 10:41 AM by Martin Isheim

    ManyToMany relationship removal example

    Scott Tamosunas Newbie

      I have been hunting around looking for an example of how to remove an entry from the join table but not remove the entities. For example, let's say I have a user table and a role table, which have a many to many relationship, so add a user_role table to manage that. In my POJO's, I setup my ManyToMany annotations with the definition on the role POJO.

      Add entries into the join table seems to work fine but trying to remove entries by removing the objects from the relevant sets doesn't seem to cause the line to be removed from the join table. So, I am looking for an example of how to properly do this: setup the POJO's and simple sample code. If anyone has this, it would be appreciated.

        • 1. Re: ManyToMany relationship removal example
          Emmanuel Bernard Master

          you probably remove it from the non managed side 'ie the mappedBy side' only

          • 2. Re: ManyToMany relationship removal example
            Scott Tamosunas Newbie

            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?

            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;
            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) {
             Set<Ad> ads = currentCampaign.getAds();
             if (null != ads && ads.size() > 0) {
             for (Ad a : ads) {

            • 3. Re: ManyToMany relationship removal example
              Emmanuel Bernard Master

              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
                Martin Isheim Newbie

                cascade=REMOVE should not be applied to ManyToMany relations,
                perhaps you want to change cascade=ALL to a lower level.