Hi,
I am having an issue with a delete I am trying to do in Hibernate. Everytime I try to delete I get an issue due to child records existing so it cannot delete the parent. I want to delete children and the parent. Here is my parent mapping:
<set name="communicationCountries" inverse="true" cascade="all,delete-orphan">
<key column="COM_ID" not-null="true" on-delete="cascade" />
<one-to-many class="com.fmr.fc.portlet.communications.vo.CommunicationCountry"/>
</set>
Here is the mapping for the child class:
<many-to-one name="communication" column="COM_ID" not-null="true" class="com.fmr.fc.portlet.communications.vo.Communication" cascade="all"/>
EDIT - When I do an insert the data is inserted into the parent and the child.
When I do an update using a new object with the ID of the object I want to modify the parent is updated but any existing children are added a second time. I cannot seem to remove children. When I retrieve an object using the ID and modify this I get an error telling me org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed. I suspect this is because in one getHibernateTemplate() call I am getting the object and I am saving it in another one and these are two different sessions?
When I do a delete I get an error because children exist. I know I am just doing something completley stupid due to lack of having a clue as to how this all works.
Here are my update and delete methods, in this case the update/save is retrieving and modifying before saving. The delete is using a new object with the same ID as the one in the DB I want to delete:
public void deleteCommunication(Communication comm) throws DataAccessException
{
getHibernateTemplate().delete(comm);
}
public void saveCommunication(Communication comm) throws DataAccessException
{
Communication existing = (Communication)getHibernateTemplate().load(Communication.class, comm.getComId());
existing.getCommunicationCountries().clear();
getHibernateTemplate().saveOrUpdate(existing);
}
UPDATE So here are my new methods but still no joy. I think my issue has to do with the children not being loaded/initialized etc. With the delete though, I cant understand why the cascading delete isn't happening.
thanks so much for your help so far. I have reached my deadline for this work already so if I don't get it fixed over the weekend I am just going to have to resort to executing HQL queries as I know that will work for me :(
public void deleteCommunication(Integer id) throws DataAccessException
{
HibernateTemplate hibernate = getHibernateTemplate();
Communication existing = (Communication)hibernate.get(Communication.class, id);
hibernate.initialize( existing.getCommunicationCountries());
hibernate.delete(existing);
}
public void updateCommunication(Communication comm) throws DataAccessException
{
HibernateTemplate hibernate = getHibernateTemplate();
Communication existing = (Communication)hibernate.get(Communication.class, comm.getComId());
hibernate.initialize( existing.getCommunicationCountries());
existing.getCommunicationCountries().clear();
hibernate.saveOrUpdate(existing);
}