-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Annotation approach for http://www.hibernate.org/118.html#A
PostPosted: Thu Mar 05, 2009 4:37 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Question:
Quote:
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?

from http://www.hibernate.org/118.html#A10

I have read through the reference documentation and can't find any reference for this subject. Can someone point me to a right direction?

Thanks.


Last edited by wei725 on Fri Mar 06, 2009 2:26 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 5:41 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
The best thing to do is map the link table also to an entity and have MTO association b/w the main entities and link entity.

Code:
Like:
TABLE_A MTM TABLE_B through TABLE_AB

Then:
Class A mapped to TABLE_A
Class B mapped to TABLE_B
Class AB mapped to TABLE_AB

Now:
AB MTO A
AB MTO B

Now A and B can have a OTM collection mapping to AB

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 1:03 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
littypreethkr wrote:
The best thing to do is map the link table also to an entity and have MTO association b/w the main entities and link entity.

Code:
Like:
TABLE_A MTM TABLE_B through TABLE_AB

Then:
Class A mapped to TABLE_A
Class B mapped to TABLE_B
Class AB mapped to TABLE_AB

Now:
AB MTO A
AB MTO B

Now A and B can have a OTM collection mapping to AB


Thanks for your input, but where is annotation? I have done the type of mapping with XML files before. Now, I want to try the annotation approach for the same case.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 1:24 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Code:
@Entity
@Table(name = "TABLE_A")
public class ClassA {

   long id;
   String name;
   Set<ClassAB> abs;
   
   @Id
   @GeneratedValue
   public long getId() {
      return id;
   }

   @Column(name = "A_NAME")
   public String getName() {
      return name;
   }

   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "A_ID", nullable = false)
   public Set<ClassAB> getAbs() {
      return abs;
   }

   //Setters
}

@Entity
@Table(name = "TABLE_B")
public class ClassB {

   long id;
   String value;
   Set<ClassAB> abs;
   
   @Id
   @GeneratedValue
   public long getId() {
      return id;
   }

   @Column(name = "B_VALUE")
   public String getValue() {
      return value;
   }
   
   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "B_ID", nullable = false)
   public Set<ClassAB> getAbs() {
      return abs;
   }

   //Setters
}

@Entity
@Table(name = "TABLE_AB")
public class ClassAB {

   long id;
   String extra;
   ClassA a;
   ClassB b;

   @Id
   @GeneratedValue
   public long getId() {
      return id;
   }

   @Column(name = "AB_EXTRA")
   public String getExtra() {
      return extra;
   }

   @ManyToOne
   @JoinColumn(name = "A_ID", nullable = false)
   public ClassA getA() {
      return a;
   }

   @ManyToOne
   @JoinColumn(name = "B_ID", nullable = false)
   public ClassB getB() {
      return b;
   }

   //Setters
}

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 2:44 am 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Thanks for your following up. I am wondering whether you understand the original question or not. It is not about entity, but a composite element.


Top
 Profile  
 
 Post subject: Re: Annotation approach for http://www.hibernate.org/118.htm
PostPosted: Mon Mar 09, 2009 3:06 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I suppose this was your question.
wei725 wrote:
Question:
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?

But if you go to that link you specified you will also see this:
http://www.hibernate.org/118.html#A10 wrote:
An alternative approach is to simply map the association table as a normal entity class with two bidirectional one-to-many associations.

I have specified the mapping for this approach. I think this one is much straight forward and simpler than the other one of using composite elements.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Re: Annotation approach for http://www.hibernate.org/118.htm
PostPosted: Fri Mar 13, 2009 1:02 am 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
littypreethkr wrote:
I suppose this was your question.
wei725 wrote:
Question:
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?

But if you go to that link you specified you will also see this:
http://www.hibernate.org/118.html#A10 wrote:
An alternative approach is to simply map the association table as a normal entity class with two bidirectional one-to-many associations.

I have specified the mapping for this approach. I think this one is much straight forward and simpler than the other one of using composite elements.


Sorry, I should make it clear that I prefer the first approach which won't create another entity.


Top
 Profile  
 
 Post subject: Re: Annotation approach for http://www.hibernate.org/118.htm
PostPosted: Fri Mar 13, 2009 1:27 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
wei725 wrote:
littypreethkr wrote:
I suppose this was your question.
wei725 wrote:
Question:
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?

But if you go to that link you specified you will also see this:
http://www.hibernate.org/118.html#A10 wrote:
An alternative approach is to simply map the association table as a normal entity class with two bidirectional one-to-many associations.

I have specified the mapping for this approach. I think this one is much straight forward and simpler than the other one of using composite elements.


Sorry, I should make it clear that I prefer the first approach which won't create another entity.


Hi wei725,

As I know, to add extra columns to many-to-many relationships, you should create another entity.

Please refer to this link
http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/


Top
 Profile  
 
 Post subject: Re: Annotation approach for http://www.hibernate.org/118.htm
PostPosted: Fri Mar 13, 2009 2:12 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
SIau_Tie wrote:

Hi wei725,

As I know, to add extra columns to many-to-many relationships, you should create another entity.

Please refer to this link
http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/


Thanks for the link. I was thinking of an equivalent annotation statement which no Java code change is required. The Java code structure in the article/blog is quite different from the section of tips & tricks. To clarify my design approach, I list my business models as the followings:
Code:
@Entity
@Table(name = "group")
public class Group implements Serializable  {

       private Integer id;
   private Set<Membership> memberships;
        ...
}

public class Membership implements Serializable {

   public enum STATUS { DEDLINCED, PENDING, APPROVED };

   private Member           member;
   private Calendar    joinDate;
        ...
}

@Entity
@Table(name = "member")
public class Member implements Serializable  {

        private Integer id;
        ...
}

Where the Membership is a many-to-many relationship between groups and members without a composite-key. I know how it can be done with a XML mapping file, but not annotation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.