I have a rather complex domain model, implemented in EMF and persisted with teneo/hibernate/MySQL. I have a subtree of my model (subtree in the sense of 'containment') where no element of the subtree is referenced from outside the subtree (only references from elements that are 'contained' in the subtree to other elements 'contained' in the subtree).
I want to delete the subtree and remove the root element from the collection it is conatained in. I expect this to work without problems as all references are inside the subtree. Anyway I get exceptions from MySQL that foreign key violations are encountered. Hence the deletion fails. Is the deletion process expected to work? Or is teneo/hibernate not that clever to remove the elements in the correct order?
The exception message:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`ide`.`equip_equipment`, CONSTRAINT `equip_equipment_usagedomain` FOREIGN KEY (`component_ud_id`) REFERENCES `ud_component` (`id`))
Update: My cascade policies are as follows:
hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTAINMENT, "ALL");
hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT, "REFRESH,PERSIST,MERGE");
hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, InheritanceType.SINGLE_TABLE.getName());
hibernateProperties.setProperty(PersistenceOptions.ADD_INDEX_FOR_FOREIGN_KEY, Boolean.TRUE.toString());