-->
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.  [ 2 posts ] 
Author Message
 Post subject: Self Many-to-Many Relationship with attributes
PostPosted: Sat Jun 20, 2009 12:32 pm 
Newbie

Joined: Sun May 24, 2009 1:01 pm
Posts: 3
Hi!

I have this scenario:

Code:
public class A
{
...
   private Set<ARelationship> relationshipsOwner;
   private Set<ARelationship> relationshipsRelated;
...
}

public class ARelationship
{
...
   private A owner;
   private A related;
...
   private TypeX typeX;
...
}

How to map this using annotations?


Top
 Profile  
 
 Post subject: Re: Self Many-to-Many Relationship with attributes
PostPosted: Mon Jun 22, 2009 2:51 pm 
Newbie

Joined: Sun May 24, 2009 1:01 pm
Posts: 3
I solved it doing this:

Code:
@Entity
@Table
public class A
{
...
   private int id;
...
   private Set<ARelationship> relationshipsOwner; //Relationships as Owner
   private Set<ARelationship> relationshipsRelated; //Relationships as Related
...
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(unique = true, nullable = false)
   public int getId()
   {
      return this.id;
   }
...
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")
   public Set<A> getRelationshipsOwner()
   {
      return this.relationshipsOwner;
   }
...
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "related")
   public Set<A> getRelationshipsRelated()
   {
      return this.relationshipsRelated;
   }
...
}

@Entity
@Table
public class ARelationship
{
...
   private int id;
   private A owner;
   private A related;
...
   private TypeX typeX;
...
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(unique = true, nullable = false)
   public int getId()
   {
      return this.id;
   }
...
   @ManyToOne(fetch = FetchType.LAZY, optional = false)
   public A getOwner()
   {
      return this.owner;
   }
...
   @ManyToOne(fetch = FetchType.LAZY, optional = false)
   public A getRelated()
   {
      return this.related;
   }
...
}


Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.