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());