I just don't understand why Hibernate throws the exception indicated in the header. I probably do not understand the idea of governing the state behind Hibernate.
I have the following situation:
One-to-Many Relationship Between Organization and Employee
Organization.hmb.xml
<set name="employees" inverse="true" cascade="save-update"> <key column="organization_id"/> <one-to-many class="Employee"/> </set>
Employee.hbm.xml
<many-to-one name="organization" class="Organization" column="organization_id" />
I use the standard Spring / Hibernate application architecture with services and DAOs, where DAOs extend the HibernateDaoSupport class and use the services of the HibernateTemplate class to manage sessions.
When I try to remove Employee in this scenario ...
Employee e=employeeService.read(1); //EDIT: Important! delete operation in EmployeeService is (@)transactional employeeService.delete(e); //this call just delegate execution to employeeDao.delete
EDIT . At the beginning, I did not mention that the delete operation in the Service layer is transactional, which seems to be important information (keep reading)!
Sleep mode throws ...
ObjectDeletedException: deleted object would be re-saved by cascade...
Deleting an operation in an EmployeeService looks like ...
@Transactional public void delete(Employee emp){ Employee e=employeeDao.read(emp.getId()); if(e==null) throw NoSuchOrganizationException(); employeeDao.delete(e); }
Scenarios (they are not related):
1. When I remove cascade = "save-update" from the relationship mapping with Employee (s) in Organization.hbm.xml, everything works fine .
2. When I delete the @Transactional annotation from the delete method, everything works fine.
3. When I delete the child (Employee) from the parent (Organization) list of children , and then do the deletion, everything works fine.
Question :
Why does Hibernate even care about the cascade in the parent class ?
Where is the execution point at which it considers the cascade of the Organization object ? Why can't he just delete Employee (Child) with DELETE FROM ... and what is he. In addition, Employee is the owner of the relationship, and the operations it performs must manage the relationship itself. When did he think that all operations on the Organization object in the specified script anyway ? I just do not understand.