[jboss-user] [EJB 3.0] - Re: ManyToMany relationship removal example

scotttam do-not-reply at jboss.com
Wed Jul 26 19:55:22 EDT 2006


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();
  | 
  | 		    }
  | 
  | 		}
  | 
  | 


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3961160#3961160

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3961160



More information about the jboss-user mailing list