Hi All,
I have discovered something with Hibernate and I need to know if it's a bug or just the way Hibernate works.
I have a parent/child situation. I have mapped the parent so that the children are a Set and I have set cascade="delete". If I have a parent object that I try to delete using
Code:
getHibernateTemplate().delete( parent )
I get an error saying that the parent still has children. That is very strange because I have set cascade to delete.
I then try to it differently. Before I call delete I try to load the object from the database using a detached criteria.
Code:
Parent parent = hibernateTemplate.findByCriteria( detachedCriteria ).get( 0 );
parent.getChildren().size();
getHibernateTemplate().delete( parent );
Now it works. It seams that the object can only delete it's children if the object was loaded in the session. Isnt hibernate just supposed to say
DELETE FROM Children WHERE Parent_id=parentId
and then
DELETE FROM Parent WHERE Id=parentId
no matter if the object has been loaded in the session?
Thanks in advance,
Martyn