Hi,
In a one-to-many relationship, I am trying to dereference a child(interface) collection and then adding a new child collection to the parent(device).
When I tried this I got the HibernateException: Don't dereference a collection with cascade="all-delete-orphan"
I looked at the following explanation
http://www.hibernate.org/264.html
because of which I tried to clear the collection and saveorupdate the object. The problem is that though the parent reference is being removed the child object is not getting deleted so when I try to now add a new child I get the error shown below.
How am I supposed to get around this? Has automatic dereferencing worked for you?
Thanks,
Son
Full stack trace of any exception that occurs:
Hibernate: select interfaced_.if_name, interfaced_.device_ip as device2_1_ from interface_table interfaced_ where interfaced_.if_name=?
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.c.nm.tio.deviceLayer.InterfaceDB#fa2]
at org.hibernate.engine.PersistenceContext.checkUniqueness(PersistenceContext.java:549)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:251)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:214)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:416)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:153)
at org.hibernate.engine.Cascades.cascade(Cascades.java:721)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:860)
at org.hibernate.engine.Cascades.cascade(Cascades.java:739)
at org.hibernate.engine.Cascades.cascade(Cascades.java:817)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.c.nm.tio.deviceLayer.TestDB.update(TestDB.java:92)
at com.c.nm.tio.deviceLayer.DiceServer.main(DiceServer.java:281)
Hibernate version: 3.0
Name and version of the database you are using:
mysql 4.1
mapping [b]
In interface xml
<many-to-one name="device" class="com.c.nm.tio.deviceLayer.DeviceDB" column="device_ip" cascade="all" />
In device xml
<set name="interfaces" cascade="all-delete-orphan" inverse="true" lazy="true" table="interface_table">
<key column="device_ip" on-delete="cascade"/>
<one-to-many class="com.c.nm.tio.deviceLayer.InterfaceDB"/>
</set>
[b] session code [b]
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
DeviceDB dev = (DeviceDB)session.load(DeviceDB.class, ipAdd);
HashSet interfaces = new HashSet();
Iterator it = ifNames.iterator();
dev.getInterfaces().clear();
session.saveOrUpdate(dev);
System.out.println("interfaces cleared");
while(it.hasNext())
{
InterfaceDB i = new InterfaceDB((String)it.next());
dev.addInterface(i);
}
session.saveOrUpdate(dev);
tx.commit();
HibernateUtil.closeSession();
[b]The generated SQL (show_sql=true):
select interfaced_.if_name, interfaced_.device_ip as device2_1_ from interface_table interfaced_ where interfaced_.if_name=?
Debug level Hibernate log excerpt: