Hello,
I'm using hibernate 3.3.1 GA version. This is my use case. I have a parent with child objects. My first call to the server returns the parent with all the child objects. The client then performs some operation and return new set of child objects. My DAO code basically reconstructs the child and set all these incoming child objects. The child object could be any one of the persisted child objects. When the code sets the child objects on the parent and update the changes, only the update/insert operation are being performed. The previous child objects are not removed from datastore. I'm pretty sure that it is a common use case and there is something that I'm missing to achieve the purpose. Could some one please point me in the right direction? Thanks.
Parent mapping:
<set name="attributes" inverse="true" cascade="all,delete-orphan">
<key column="USR_RPT_I"/>
<one-to-many class="com.ejgallo.icc.ussc.bo.UserReportAttribute"/>
</set>
Child mapping:
<many-to-one name="userReport" column="USR_RPT_I" not-null="true"/>
Code to persist the changes:
Service layer build the persistent object and sends it to the DAO to persist the changes.
service layer:
UserReport report = new UserReport();
report.setId(<previously persisted id>);
Set attributes = new HashSet();
UserReportAttribute attr = new UserReportAttribute();
attr.setId(<previously persisted id>);
attributes.add(att);
UserReportAttribute attr2 = new UserReportAttribute();
attr2.setId(<new id..not yet persisted>);
attributes.add(attr2);
report.setAttributes(attributes);
calls DAO with the persistence capable object, report.
DAO layer:
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
HibernateUtil.getSessionFactory().getCurrentSession().update(report);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
Say for eg. this report with id with 5 child attributes, and on this update process, I'm sending one new attribute and 1 updated attribute. So I'm expecting the hibernate to issue 4 delete query, 1 insert query, 1 update query. However, hibernate issues only 1 insert, 1 update. It does noto issue the delete queries. What I'm missing here?
Thanks for your time.
|