0

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());
4

1 回答 1

0

仅当您已将关联配置为级联删除(删除)操作时,它才会起作用:

@OneToMany(cascade = CascadeType.REMOVE)
private Set<Child> children;

如果您没有此级联,则删除父实体只会删除此特定实体,当然,由于所有子实体都具有父实体的外键,因此您将获得约束违反异常。

于 2012-06-06T09:30:30.640 回答