Project

General

Profile

bug #10101

Updated by Andreas Müller over 1 year ago

We currently have 3 sorted tree structures in the CDM: Classifications, TermTrees and PolytomousKeys. 

 The correct mapping and handling of sorted tree structures is difficult. Children are modelled as Lists which usually is best handled with an @OrderColumn annotation (#3722) and a bidirectional mapping with orphanRemoval. If orphanRemoval is not used orphaned (deleted) children need to be deleted manually, otherwise they stay in the database and, in the worst case, have the same parent_id and sortindex if parent_id is not correctly removed by bidirectionality imlementation. This leads to kind of duplicates and, even worse, to gaps in the sortindex when the tree structure is persisted via merge. Gaps in the sortindexes (e.g. 0 followed by 3) lead to null values when the list is reloaded (#5536, #8127). 

 However, though orphanRemoval handles this best for Lists it is not good for (recursive) trees because moving a subtree to another parent then requires to clone the full subtree as not only the root but also all its children will be automatically deleted by hibernate via orphanRemoval. For large trees this is unwanted behavior and also for smaller trees cloning is not a great solution. 
 
 

 So a better solution is to manually remove the nodes if they are not used anymore. We tried to do this automatically for merge via the PostMergeEntityListener but still without success. See comments in #note-7.  

 Therefore a new merge method was created to merge together with the entities that should be deleted. This seems to work. 

 Note: To avoid the sortindex still has a gap for some reason we have to touch the children list somehow before commit. This is currently done in the PostMergeEntityListener. 

 Related tests are: PolytomousKeyNodeServiceTest.testMergeXXX()    (maybe will move to PolytomousKeyServiceTest partly)

Back