[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1395) Hierarchical entity data management using 'modified preorder tree traversal algorithm' (nested set trees)

Joonas Koivunen (JIRA) noreply at atlassian.com
Tue Nov 17 04:21:09 EST 2009


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1395?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=34633#action_34633 ] 

Joonas Koivunen commented on HHH-1395:
--------------------------------------

Actually I've found that the post-update event _is_ the way to go.

I'm yet to formulate a proper patch against the Christians version above, but the solution is:

 * add post-load -eventlistener, catch NestedSetNode<?> entities, set a property "persistentParent"
  {code}
if (event.getEntity() instanceof NestedSetNode<?>) 
  (NestedSetNode)event.getEntity().setPersistentParent( ((NestedSetNode)event.getEntity()).getParent());
{code}
 * add post-update -eventlistener, which uses the "persistentParent" property to first find out whether or not the parent has changed
 ** if the parent *was* null but is no longer, joins the node into parent's tree (sort of like insert operation)
 ** if the parent *was* not null but is null, detaches the node
 ** if the parent *was* not null but is not null and not same, detaches the node and joins

The detach operation has 3 operations:
 # set new ns_thread and add correction *c* to ns_left and ns_right, where {{long c = 1 - node.getNsLeft();}}
 # fix previous threads nodes TO THE RIGHT (excluding previous parents of node): 
 #* add correction c to both ns_left and ns_right, where {{long c = -2 * updatedInThePreviousQuery;}}
 # fix previous thread parent hierarchy (all parents in the previous node):
 #* add correction c to ns_right (c is the same)

The join operation has 4 operations:
 # setup the joined tree:
 #* add correction c to both ns_left and ns_right, where {{ long c = newParent.getNsRight() - 1; }}
 # fix up the new parent tree with nodes TO THE RIGHT by the insertion point
 #* add correction c to both ns_left and ns_right, where {{ long c = 2 * updatedInThePreviousQuery;}}
 # fix up the new parent trees new parents
 #* add correction c to ns_right (c is the same)
 # fix the joined tree thread_id as new tree's thread id

Simple as that :) Watch this space for a patch.

Also, if someone was interested in formulating a hibernate-3.5 style tests for this one maybe perhaps this might get someday into main tree.. I've got no idea how to do those, as in fact I haven't yet look at any of the tests in the 3.5 series..

> Hierarchical entity data management using  'modified preorder tree traversal algorithm' (nested set trees)
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: HHH-1395
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1395
>             Project: Hibernate Core
>          Issue Type: New Feature
>          Components: core, query-hql
>         Environment: Hibernate 3.1 plus Annotations extending EJB3 implementation
>            Reporter: James Salt
>         Attachments: nestedset.tar.gz
>
>   Original Estimate: 3 weeks
>  Remaining Estimate: 3 weeks
>
> An enhancement to allow entities to exist in a managed tree (DAG) structure, via the use of property annotations and entity annotations. With an extension to the HQL language to mimic the Oracle recusive query clause. 
> This would be classically be useful for Bill of Material uses, or any tree like entity structure. 
> I have implemented a homebrew approach using the 'adjacency list method' , and a post persist call back method to set the node path. Although I think a more full blown optimised algorithm would be the 'modified preorder tree traversal algorithm'. 
> In the annotations world this could be implemented using an annotation like @Hierarchical to the entity, with a @Parent (mask for @ManyToOne) annotation on the parent field/parameter and @Children (mask for @OneToMany) annotation on the list of children entities. 
> Then a postupdate method could be implemented for the entity type, that recalculates the tree meta data -i.e. parent, left and right. 
> I believe this would be a valuable extension (useful for many common data models) to the hibernate core product that could be exposed through annotations as I have discussed and the hbm.xml. 
> The users of such a system would be aware of the performance implications on insert/update/delete, but gain a massive performance gain on querying a tree data model. 
>   

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       



More information about the hibernate-issues mailing list