6 Replies Latest reply on Jul 30, 2006 4:24 PM by abl

    How to access a join table column?!

    fabiorusso

      Hello. I hope this is not a dumb question. I have two Entities with a ManyToMany bidirectional relationship. Up to this point everything is ok. But there is a column in the join table I would like to access and I really don't know how :-(

      Here are my Entities: the Partner, the OptionRate and the join table. There is a column named 'isDefault' in the join table that I need to check.

      Here is my code:
      This is the Partner entity:

      @Entity
      @Table(name="VOW_PARTNERS")
      public class AbbeyphonePartner implements Serializable
      {
       private String partnerId;
       private String passKey;
       private String partnerDescription;
       private List <RateOption> rateOptions;
      
       @Id
       @Column(name="PARTNER_ID")
       public String getPartnerId()
       {
       return this.partnerId;
       }
      
       public void setPartnerId(String partnerId)
       {
       this.partnerId = partnerId;
       }
      
       @Column(name="PARTNER_DESCRIPTION")
       public String getPartnerDescription()
       {
       return this.partnerDescription;
       }
      
       public void setPartnerDescription(String partnerDescription)
       {
       this.partnerDescription = partnerDescription;
       }
      
       @Column(name="PASS_KEY")
       public String getPassKey()
       {
       return this.passKey;
       }
      
       public void setPassKey(String passKey)
       {
       this.passKey = passKey;
       }
      
       @ManyToMany(fetch = FetchType.EAGER , mappedBy ="partners")
       public List<RateOption> getRateOptions()
       {
       return rateOptions;
       }
      
       public void setRateOptions (List <RateOption> options)
       {
       this.rateOptions = options;
       }
      }
      

      and the RateOption Entity:
      @Entity
      @Table(name="BILLING_RATEOPTIONS")
      public class RateOption implements java.io.Serializable
      {
       private Integer rateoptionId;
       private Integer calculationalgorithmId;
       private Integer priceprofileId;
       private String description;
       //private char isdefault;
       private List <AbbeyphonePartner> partners;
      
       @Id
       @Column (name="RATEOPTION_ID")
       public Integer getRateoptionId()
       {
       return this.rateoptionId;
       }
      
       public void setRateoptionId(Integer rateoptionId)
       {
       this.rateoptionId = rateoptionId;
       }
      
       @Column (name="CALCULATIONALGORITHM_ID")
       public Integer getCalculationAlgorithmId()
       {
       return this.calculationalgorithmId;
       }
      
       public void setCalculationAlgorithmId(Integer calculationalgorithmId)
       {
       this.calculationalgorithmId = calculationalgorithmId;
       }
      
       @Column (name="DESCRIPTION")
       public String getDescription()
       {
       return this.description;
       }
      
       public void setDescription(String description)
       {
       this.description = description;
       }
      
       @Column (name="PRICEPROFILE_ID")
       public Integer getPriceprofileId()
       {
       return this.priceprofileId;
       }
      
       public void setPriceprofileId(Integer priceprofileId)
       {
       this.priceprofileId = priceprofileId;
       }
      
      // @Column(name="ISDEFAULT")
      // public char getIsdefault()
      // {
      // return this.isdefault;
      // }
      //
      // /**
      // * @param isdefault the isdefault to set
      // */
      // public void setIsdefault(char isdefault)
      // {
      // this.isdefault = isdefault;
      // }
      
       @ManyToMany(fetch = FetchType.EAGER)
       @JoinTable (name="VOW_PARTNER_RATEOPTIONS", //TODO: HERE!
       joinColumns={@JoinColumn (name="RATEOPTION_ID")},
       inverseJoinColumns={@JoinColumn (name="PARTNER_ID")})
       public List<AbbeyphonePartner> getPartners()
       {
       return this.partners;
       }
      
       public void setPartners(List<AbbeyphonePartner> partners)
       {
       this.partners = partners;
       }
      }
      


      The join table would be like this:
      - RATEOPTION_ID Integer not null
      - PARTNER_ID Integer not null
      - ISDEFAULT char(1) // can be Y or N.

      I need that ISDEFAULT field but I really don't know how to do it. Thank you very much for any help you can supply.


        • 1. Re: How to access a join table column?!
          abl

          you have to define the join table yourselve as entity - something like:

          @Entity
          public class PartnerToRate ...
          {
           int partnerId;
           int rateOptionId
           boolean default;
          
          ...
          }

          and use "mappedBy" attribute. this way we defined all our ManyToMany relations cause it's more flexible and extensible.

          • 2. Re: How to access a join table column?!
            fabiorusso

            Thanks. Do I still maintain the old code "as is" and simply put the mappedBy in the new PartnerToRate entity? I'm sorry if the question is dumb but there is not much documentation yet on this topic :-/ and I am sort of a newbie to ejb3.

            So let me see if I understood this well:

            1) I keep the ManyToMany mapping between Partner and OptionRate
            2) I add the third entity representing the joinTable and put a mappedBy attribute somewhere in it...

            Right?

            Could you please provide a little code as an example of this? Thank you very, very much :-)

            • 3. Re: How to access a join table column?!
              abl

              no - you have to deal with your m:m entity. you will have something like (was misleading in first post - sorry):

              @Entity
              class PartnerToRate
              {
               Partner partner;
               ...
              
               @ManyToOne
               Partner getPartner()
               {
               return partner;
               }
              ...
              }

              and in Partner:

              @OneToMany( mappedBy="partner")
              List<PartnerToRate> getPartnerToRates()
              {
               return partnerToRates;
              }

              of course you can add additional getter like:

              @Transient
              List<Partner> getPartners()
              {
               List<Partner> ret = new Arraylist<Partner>();
               for( PartnerToRate p2r : partnerToRates)
               ret.add( p2r.getPartner());
              
               return ret;
              }

              ok?

              • 4. Re: How to access a join table column?!
                fabiorusso

                Ok! Now it's clear! So I do m:jointable and jointable:m
                That's it then. So I can of course also map the OptionRates in the PartnerToRate with:

                @ManyToOne
                 Partner getOptionRate()
                 {
                 return optionrate;
                 }
                

                and in the OptionRate entity with:
                @OneToMany( mappedBy="optionrate")
                List<PartnerToRate> getPartnerToRates()
                {
                 return partnerToRates;
                }
                


                So from now on I know I can use the Many2Many only if I don't need to access any column in the join table and have to use the Many2One with a join entity in the middle otherwise.

                I will try this immediately. :-)

                Thank you very much!! Your code was very helpful.

                • 5. Re: How to access a join table column?!
                  scotttam

                  For the join object, don't you need to define a composite primary key, otherwise you will get a "No identifies exists for specific entity" error on startup?

                  • 6. Re: How to access a join table column?!
                    abl

                    of cource you are right - a composite key or just a additional primary key for the entity.