About Treatment Tree Structure in Hibernate

xiaoxiao2021-03-06  36

1:

We all know that the Tree structure is a dead point in RDBMS. However, we often encounter such structures in practice, such as departmental structures, column structure .... The original use of JDBC to handle these trees structure is relatively simple, just solve a key problem that selects a tree, then sort I will get it. How to complete these work efficiently in Hibernate? Here is a typical parent-child relationship mapping

Java code:

<μ / id>

But such a structure, if you want to get all the nodes under a child, do you have multiple SQL (a few layers of trees to be toned several times) Have more efficient implementation? 2: I just check the first layer when I did it, and then I can be a recursive for this root.

Java code:

private void buildChild (FolderListForm folderListForm, DocPO docPO) throws Exception {Collection children = docPO.getChildren (); int order = 1; for (java.util.Iterator it = children.iterator (); it.hasNext ();) { DocPO child = (DocPO) it.next (); docTreeNodeForm docTreeNodeForm = new docTreeNodeForm (); BeanUtils.copyProperties (docTreeNodeForm, child); folderListForm.getDocTreeNodeFormList () add (docTreeNodeForm);. buildChild (folderListForm, child);}}

3: Please note that Parent and Children are mapped to the same field fk_parend_id, and the various operations of the tree can be successful, and Hibernate has to be strong! Java code:

Import java.util.list; import java.util.Arraylist; import java.util.map; import java.util.stack;

/ ** * @ hibernate.class table = "cats" * Dynamic-update = "true" * Dynamic-insert = "true" * * / public class cat {private long id; // identifier private list children = new arraylist ( Private cat parent;

/ ** * @ hibernate.id * generator-class = "native" * column = "catch_id" * / public long getId () {return id;} private void setid (long id) {this.id = ID;} PUBLIC Cat addChildren (Cat Child) {Children.Add (Child); Return this;}

/ ** * @ hibernate.list * cascade = "all" * lazy = "true" * @ hibernate.collection-key * column = "fk_parent_id" * @ hibernate.collection-one-to-many * class = "COM. Nosqldb.bo.cat "* @ hibernate.collection-index * column =" index_parent_id "* / public list getchildren () {return children;}

/ ** * @Param Children the children to set. * / Public void setchildren (list children) {this.children = children;}

/ ** * @Return * @ hibernate.many-to-one * column = "fk_parent_id" * class = "com.nosqldb.bo.cat" * cascade = "all" * / public catch getparent () {Return Parent; } / ** * @Param Parent the Parent to set. * / Public void setparent (cat parent) {this.parent = parent;

/ ** * Tree traversal * Do not recry, with stack. * Here is just as an example, I don't recommend encapsulating business logic in the Entity layer. * / Public list getvisitsults () {list l = new arraylist (); stack s = new stack (); s.push (this); while (s.empty () == false) {cat c = (cat) s.POP (); l.Add (c); list children = C.GetChildren (); If (children! = Null) {for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-66510.html

New Post(0)