Ok. This is a bit of a hack, but I'm trying code around my problem with cascade-delete-orphan. However, when I execute this code, I get the following exception. How am I "dereferencing" the collection? I checked the JDK 1.4 implementation of AbstractMap.putAll and it simply iterates over the given map and copies in the key/value mappings.
Exception
net.sf.hibernate.HibernateException:
You may not dereference a collection with cascade="all-delete-orphan"
at net.sf.hibernate.impl.SessionImpl.updateUnreachableCollection(SessionImpl.java:2885)
at net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2754)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2225)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
Code
Code:
sess = sessionFactory.openSession();
trans = sess.beginTransaction();
if( parent.getId() != null )
{
loaded = (Parent) sess.load(ParentImpl.class, parent.getId());
loaded.copyFrom(parent);
sess.update(loaded);
} else
{
sess.save(parent);
}
trans.commit();
Code:
public void copyFrom(Parent parent)
{
// copy all of the target parent's attributes
setData(data);
// remove children that are not part
// of the parent we are copiing
Iterator it = children.keySet().iterator();
String key;
while( it.hasNext() )
{
key = (String) it.next();
if( ! parent.getChildren().containsKey(key) )
{
children.remove(key);
}
}
// copy in all of the children from the
// target parent
children.putAll(parent.getChildren());
}