Hi all,
I have a very strange Hibernate problem with Cascade="all-delete-orphan". As premise, I expose some DAO functionalities as Web Service with Apache CXF and Spring. In particular I have this service interface:
Code:
@WebService
public class RawDataServiceImpl implements RawDataService {
private RawDataDao rawDataDao;
@Override
public void updateRawData(RawData rawData) {
try {
rawDataDao.saveRawData(rawData);
} catch (DaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class RawDataDaoHibernateImpl implemenets RawDataDao extends HibernateTemplate {
public void saveRawData(RawData rawData) {
getHibernateTemplate().saveOrUpdate(rawData);
}
}
As database structure, I have one entity parent (RawData) and one entity child (ChangeRequest), mapped as following:
Code:
<hibernate-mapping package="it.ids.plx.model.bean">
<class name="RawData" table="RAWD_RAWDATAS">
<id column="RAWDATA_ID" name="id">
<generator class="native"/>
</id>
<property column="DESCRIPTION" generated="never" lazy="false"
name="description" type="string"/>
<property column="SUMMARY" generated="never" lazy="false"
<set name="changeRequests" table="RAWD_CHANGE_REQUESTS" cascade="all-delete-orphan" lazy="false">
<key column="RAWDATA_ID"/>
<one-to-many class="it.ids.plx.model.bean.ChangeRequest"/>
</set>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------
<hibernate-mapping package="it.ids.plx.model.bean">
<class name="ChangeRequest" table="RAWD_CHANGE_REQUESTS">
<id column="CHANGEREQ_ID" name="id">
<generator class="native"/>
</id>
<property name="comments" column="COMMENTS"></property>
</class>
</hibernate-mapping>
What it's happening is that, when I execute this code in the WebService client:
Code:
List<RawData> rawDatas = rawDataService.findAllRawDatas();
RawData rawData = rawDatas.get(0);
rawData.getChangeRequests().clear();
rawDataService.updateRawData(rawData);
I find all the instance of ChangeRequest with RAWDATA_ID = null instead of zero elements,
only if this call is done via WebService. If I do this call in Unit Test (e.g. AbstractAnnotationAwareTransactionalTests or AbstractDependencyInjectionSpringContextTests) it works fine.
These are the logs of the two identical calls, the first with
web service, the second in
unit test:
Code:
12/mag/09-09:59:20,690 [DEBUG] SQL:111 - /* update it.ids.plx.model.bean.RawData */ update RAWD_RAWDATAS set DESCRIPTION=?, SUMMARY=?, CREATION_DATE=?, SUBMISSION_DATE=?, EXP_RES_DATE=?, AUTHOR_ID=?, RAWDATA_TYPE_ID=?, RAWDATA_STATUS_ID=?, WORKFLOW_ID=? where RAWDATA_ID=?
12/mag/09-09:59:20,690 [DEBUG] AbstractCollectionPersister:1032 - Deleting collection: [it.ids.plx.model.bean.RawData.changeRequests#66]
12/mag/09-09:59:20,706 [DEBUG] SQL:111 - /* delete one-to-many it.ids.plx.model.bean.RawData.changeRequests */ update RAWD_CHANGE_REQUESTS set RAWDATA_ID=null where RAWDATA_ID=?
12/mag/09-09:59:20,706 [DEBUG] AbstractCollectionPersister:1089 - done deleting collection
12/mag/09-09:59:20,706 [DEBUG] AbstractCollectionPersister:1112 - Inserting collection: [it.ids.plx.model.bean.RawData.changeRequests#66]
12/mag/09-09:59:20,706 [DEBUG] AbstractCollectionPersister:1200 - collection was empty
12/mag/09-09:59:20,753 [DEBUG] SessionImpl:401 - disconnecting session
-------------------
Code:
12/mag/09-10:01:51,313 [DEBUG] SQL:111 - /* update it.ids.plx.model.bean.RawData */ update RAWD_RAWDATAS set DESCRIPTION=?, SUMMARY=?, CREATION_DATE=?, SUBMISSION_DATE=?, EXP_RES_DATE=?, AUTHOR_ID=?, RAWDATA_TYPE_ID=?, RAWDATA_STATUS_ID=?, WORKFLOW_ID=? where RAWDATA_ID=?
12/mag/09-10:01:51,329 [DEBUG] AbstractCollectionPersister:1032 - Deleting collection: [it.ids.plx.model.bean.RawData.changeRequests#66]
12/mag/09-10:01:51,329 [DEBUG] SQL:111 - /* delete one-to-many it.ids.plx.model.bean.RawData.changeRequests */ update RAWD_CHANGE_REQUESTS set RAWDATA_ID=null where RAWDATA_ID=?
12/mag/09-10:01:51,329 [DEBUG] AbstractCollectionPersister:1089 - done deleting collection
[b]12/mag/09-10:01:51,329 [DEBUG] SQL:111 - /* delete it.ids.plx.model.bean.ChangeRequest */ delete from RAWD_CHANGE_REQUESTS where CHANGEREQ_ID=?[/b]
[b]12/mag/09-10:01:51,329 [DEBUG] SQL:111 - /* delete it.ids.plx.model.bean.ChangeRequest */ delete from RAWD_CHANGE_REQUESTS where CHANGEREQ_ID=?[/b]
As you can see, in the second log, the child row is deleted as I want...
I know this problem can be caused from a bunch of factors...
I hope you have any suggestion to help me to solve the problem. Thanks