When I implement the composite pattern, hibernate gives the following error:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: logic.StdComposite
I read on the Hibernate site:(
http://www.hibernate.org/117.html?cmd=prntdoc)
Quote:
Hibernate throws: Another object was associated with this id
For a particular session, there may be at most one object that represents a particular database row. So, if you already loaded an object with a particular identifier, you can't then try to associate a different object with the same identifier by calling update().
If you absolutely must do this, evict() the original instance first - but it is much better to simply avoid this situation.
But how is it possible then to implement the composite pattern?
For example:
Component c1 = new Component(...);
Component c2 = new Component(...);
c1.add(c2); //for example: c2 is the child of c1, but the objects c1 and c2 have the same class types and will be stored in the same table.
Why does hibernate not automatically assign different id's for the two objects?
How can I solve this problem?
Thanx in advance...
Stijn.