2 Replies Latest reply on Mar 25, 2006 10:29 AM by epbernard

    @JoinTable - table contents being cleared when a new object

    scotttam

      I have a simple Many to Many relationship:

      Campaigns can belong to many Markets
      Markets can be associated to many Campaigns

      therefore, I have a join table campaign_market_xref

      My entity beans looks like (abbreviated):

      @Entity
      @Table(name="campaign")
      public class Campaign implements Serializable {
      
       @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy="campaigns")
       public Set<Market> getMarkets() {
       return markets;
       }
      
       public void setMarkets(Set<Market> markets) {
       this.markets = markets;
       }
      }
      


      @Entity
      @Table(name="market")
      public class Market implements Serializable {
      
       @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
       @JoinTable(table = @Table(name = "campaign_market_xref"),
       joinColumns = {
       @JoinColumn(name = "market_id")
       },
       inverseJoinColumns = {
       @JoinColumn(name = "campaign_id")
       }
       )
      
       public Set<Campaign> getCampaigns()
       {
       return campaigns;
       }
      
       public void setCampaigns(Set<Campaign> campaigns)
      
       {
       this.campaigns = campaigns;
       }
      }
      



      Markets are generally static... these are a predefined list that will change very infrequently. Campaigns on the other hand will be created often.

      So, when I create a new campaign and select multiple markets for the campaign, I see the association in the join table. However, as soon as I create a new campaign, the old associations for the other campaign are deleted. To remedy this, I added logic in the Campaign create method to determine all the campaigns that market is associated with and persist that again, however there seems to me there must be an easier way to do this.

      Am I missing an annotation on the JoinTable or the Entity bean that will cause the join table to be preserved for all other associations?