Hi,
I've an issue when trying to handle a parent-child-relationchip.
It all works well for the the case that I set the children and update the parent "p" afterwards:
Code:
SessionUtil.beginTransaction();
Session session = SessionUtil.getSession();
// recommended approach of Christian Bauer
// p = parent, Kunden and Projektmitarbeiter are Sets
p.getKunden().clear();
p.getProjektmitarbeiter().clear();
p.getKunden().addAll(neue_kunden);
p.getProjektmitarbeiter().addAll(neue_mitarbeiter);
session.update(p);
The problem is that only new children are saved, but those who are not included in the collection anymore are not deleted. Likewise, those children who have been modified are not updatet -
is this due to a bad equals()-method?. My mapping has the appropiate cascade="all-delete-orphan" - attribute, however.
To avoid this problem of not-deleted-childeren I thought I'd have to manually delete them.
To do this, I firstly have to grab the old set.
Now the code looks as follows but I get the NonUniqueObjectException -- astonishingly, obviously my parent "p" is not unique anymore, nut why??
This is the code that rises the error:
Code:
SessionUtil.beginTransaction();
Session session = SessionUtil.getSession();
Set<Kunde> alte_kunden = this.getKunden(p);
Set<Projektmitarbeiter> alte_mitarbeiter = this.getProjektmitarbeiter(p);
// session.save(p); session.flush() would solve the Exception, but I don't wont to have this row inserted again!
// recommended approach of Christian Bauer
// p = parent, Kunden and Projektmitarbeiter are Sets
p.getKunden().clear();
p.getProjektmitarbeiter().clear();
p.getKunden().addAll(neue_kunden);
p.getProjektmitarbeiter().addAll(neue_mitarbeiter);
session.update(p);
Hibernate version: 3.05
Full stack trace of any exception that occurs:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.mdpro.protokolldb.models.Projekt#111]
at org.hibernate.engine.PersistenceContext.checkUniqueness(PersistenceContext.java:586)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:254)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:214)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:490)
log excerpt
2005-06-24 12:32:06,834 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.mdpro.protokolldb.models.Person#523]
2005-06-24 12:32:06,834 DEBUG [org.hibernate.engine.PersistenceContext] initializing non-lazy collections
2005-06-24 12:32:06,834 DEBUG [org.hibernate.jdbc.JDBCContext] after autocommit
2005-06-24 12:32:06,834 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection
2005-06-24 12:32:06,834 DEBUG [org.hibernate.jdbc.ConnectionManager] closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets:0, globally: 0)]
2005-06-24 12:32:06,834 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] returning connection to pool, pool size: 1
2005-06-24 12:32:06,834 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] updating detached instance
2005-06-24 12:32:06,834 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] updating [com.mdpro.protokolldb.models.Projekt#111]
2005-06-24 12:32:06,834 WARN [org.apache.struts.action.RequestProcessor] Unhandled Exception thrown: class org.hibernate.NonUniqueObjectException
2005-06-24 12:32:06,835 DEBUG [com.mdpro.protokolldb.database.SessionUtil] Closing Session of this thread.
2005-06-24 12:32:06,835 DEBUG [org.hibernate.impl.SessionImpl] closing session
2005-06-24 12:32:06,836 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/ProtokollDatenbank].[action]] Servlet.service() for servlet action threw exception
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.mdpro.protokolldb.models.Projekt#111]