-->
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.  [ 3 posts ] 
Author Message
 Post subject: Self-referencing many to many relationship valid?
PostPosted: Mon Apr 25, 2005 5:48 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Does anyone know if it's valid to have a self-refencing many to many relationship? In other words, have an object which contains a list of objects of the same type such as what I have below. As far as I can tell, it seems to work (amazingly!). I'm wondering if this is an OK thing to do, or, if I'm asking for trouble (normally, isn't there supposed to be an "inverse side"?). Does something like this have a name? I tried to look it up in the manual, but, I don't know what to look under.

Hibernate version: hibernate-3.0.1, hibernate-annotations-3.0beta1

Mapping documents:

Code:
@Entity
public class Item {
   private Collection<Item> items = new ArrayList<Item>();

   @ManyToMany
   public Collection<Item> getItems() {
      return items;
   }

   public void setItems(Collection<Item> items) {
      this.items = items;
   }
}


Code between sessionFactory.openSession() and session.close():

Code:
Item a = new Item("A");
Item b = new Item("B");
Item c = new Item("C");
a.getItems().add(b);
a.getItems().add(c);

Transaction t = s.beginTransaction();
s.persist(b);
s.persist(c);
s.persist(a);
t.commit();

System.out.println(a);
System.out.println(a.getItems());


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 11:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sure, that's valid. The hibernate test suite has such monsters :)

Yes, one side should be marked as inverse to avoid unecessary DML statements from being executed against the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 8:10 am 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Thanks. How would you mark one side as inverse though? Do you have to do this using an XML mapping file, or, is there a way to do it using annotations? I've used the mappedBy attribute previously, but, I'm not sure how that would work since the object is one and the same. I'm wondering if maybe Hibernate is smart enough to do this by default?


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