4 Replies Latest reply on May 10, 2006 10:57 AM by mazz

    How to write @ManyToMany/@JoinTable if join table has third

    gus888

      Hi All,

      I have a question on how to write @ManyToMany and @JoinTable code, e.g. a join table is

      table member_party (
      member_id,
      party_id,
      joined_date)

      how to modify the following codes:
      @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
       @JoinTable(table = @Table(name = "member_party"),
       joinColumns = {@JoinColumn(name = "MEMBER_ID")},
       inverseJoinColumns = {@JoinColumn(name = "PARTY_ID")})
      
      Many thanks for any help!

        • 1. Re: How to write @ManyToMany if join table has third attribu
          gus888

          Hi all,

          Sorry for my question is unclear. My question is: if join table has extra attributes besides the two foreign key (shown above), how to map its @ManyToMany code in entity bean. Thank you very much in advance.

          • 2. Re: How to write @ManyToMany/@JoinTable if join table has th
            gus888

            Hi all,

            I found a little idea in the Hibernate in Action book, it is on the section of Using a collection of components for a many-to-many association in Chapter 6. It seems that I need to create a new class for the joint table. Any further suggestion is appreciated.

            GUS

            • 3. Re: How to write @ManyToMany/@JoinTable if join table has th
              chrismalan

              Hi Gus,

              I see you posted in March. You may never see this response.

              Yes, you have to explicitly create another @Entity bean, say MembershipDetails

              Besides that, create a bean with an @Embeddable annotation as a composite primary key

              @Embeddable
              public class PartyMemberPK implements Serializable{
               private String member_id, party_id;
              
               public PartyMemberPK(){}
               public PartyMemeberPK(String member_id, String party_id){
               use your getters and setters to set these
               }
              }
              


              Then use PartyMemberPK as a composite PK in MembersipDetails and add the date you want, as well.

              Hope this helps.

              • 4. Re: How to write @ManyToMany/@JoinTable if join table has th
                mazz

                I've given up using @ManyToMany. I started out with them and they work fine, but I always ended up needing to put in extra attributes thus running into this problem.

                Now, even for my many-to-many relationships, I start out with @ManyToOne/@OneToMany with an explicity @Entity for the association table in the middle.