026 Hibernate operation tree structure

Source: Internet
Author: User

Tree structure: That is, the directory structure, with the parent directory, subdirectories, files and other information, and in the program tree structure is just called nodes.

A tree has a root node, and the root node has one or more child nodes, and a child node has only one parent node (outside the current root node), and there is one or more child nodes.

In other words, the tree structure, the focus is the node, that is, we need to care about the node object.

Node: A node has an ID, a name, the parent node it belongs to (the root node has no parent node or null), and other information such as one or more child nodes.

Hibernate extracts the nodes into entity classes, which are "many-to-one" mappings relative to the parent node, and the nodes are one-to-many mappings in relation to the child nodes.

Node entity class:

/** * Node * *

Public class Node {

Private int ID; Identifier

Private String name; Node name

Private int level; level, in order to output the design

Private Boolean leaf; // is the leaf node, which is designed for efficiency, dispensable

    // parent node: Because multiple nodes belong to a parent node, the Hibernate The mapping relationship says yes " Many-to-one "

Private Node parent;

    // child nodes: Because a node has more than one child node, Hibernate The mapping relationship says yes " One -to-many "

Private Set Children;

Public int getId () {

return ID;

}

Public void setId (int id) {

this. id = ID;

}

Public String GetName () {

return name;

}

Public void setName (String name) {

this. Name = name;

}

Public int Getlevel () {

return level;

}

Public void setLevel (int level) {

this. Level = level;

}

Public boolean isleaf () {

return leaf;

}

Public void Setleaf (boolean leaf) {

this. Leaf = leaf;

}

Public Node getParent () {

return parent;

}

Public void setParent (Node parent) {

this. Parent = parent;

}

Public Set GetChildren () {

return children;

}

Public void Setchildren (Set children) {

this. Children = children;

}

}

Node mapping file:

<class name="Com.wjt276.hibernate.Node" table="T_node">

<id name="id " column="id" >

<generator class="native"/>

</id>

<property name="name"/>

<property name="level"/>

<property name="leaf"/>

<!-one-to-many: joins a foreign key, referring to the current table T_node primary key

The attribute parent type is node, which is the current class, and this field is added to the same table, referencing the table's primary key--

<many-to-one name="parent" column="pid"/>

<!--<set> tag is a one-to-many way of mapping, adding a foreign key, referring to the primary key. -

<set name="Children" lazy="Extra" inverse="true">

<key column="pid"/>

<one-to-many class="Com.wjt276.hibernate.Node"/>

</set>

</class>

Test code:

Public class NodeTest extends TestCase {

The existence of a test node

Public void testSave1 () {

Nodemanage. Getinstanse (). CreateNode ("F:\\java\\javaproject\\hibernate\\hibernate_training_tree");

}

Load the test node

Public void Testprintbyid () {

Nodemanage. Getinstanse (). Printnodebyid (1);

}

}

The corresponding class code:

public class Nodemanage {

private static Nodemanage nodemanage= new Nodemanage ();

Private Nodemanage () {}//to use Singleton, so its construction method is privatized

Provide an interface to the outside

public static Nodemanage Getinstanse () {

return nodemanage;

}

/**

* Create tree

* @param FilePath need to create the root directory of the tree directory

*/

public void CreateNode (String dir) {

Session session = NULL;

try {

Session = Hibernateutils.getsession ();

Session.begintransaction ();

File root = new file (dir);

Because the first node does not have a parent node because it is null

This.savenode (root, session, NULL, 0);

Session.gettransaction (). commit ();

} catch (Hibernateexception e) {

E.printstacktrace ();

Session.gettransaction (). rollback ();

} finally {

Hibernateutils.closesession (session);

}

}

/**

* Save Node object to database

* files corresponding to the @param file node

* @param session session

* @param Parent Parents node

* @param level

*/

public void Savenode (file file, session session, Node parent, int. level) {

if (file = = NULL | |!file.exists ()) {

Return

}

Returns true if the file is a leaf node, otherwise a directory, non-leaf node

Boolean isleaf = File.isfile ();

Node node = new node ();

Node.setname (File.getname ());

Node.setleaf (IsLeaf);

Node.setlevel (level);

Node.setparent (parent);

Session.save (node);

To iterate through subdirectories

file[] Subfiles = File.listfiles ();

if (subfiles! = null && subfiles.length > 0) {

for (int i = 0; i < subfiles.length; i++) {

This.savenode (Subfiles[i], Session, node, level + 1);

}

}

}

/**

* Output tree structure

* @param ID

*/

public void Printnodebyid (int id) {

Session session = NULL;

try {

Session = Hibernateutils.getsession ();

Session.begintransaction ();

Node node = (node) session.get (Node.class, 1);

Printnode (node);

Session.gettransaction (). commit ();

} catch (Hibernateexception e) {

E.printstacktrace ();

Session.gettransaction (). rollback ();

} finally {

Hibernateutils.closesession (session);

}

}

private void Printnode (node node) {

if (node = = null) {

Return

}

int level = Node.getlevel ();

if (Level > 0) {

for (int i = 0; I < level; i++) {

System.out.print ("|");

}

System.out.print ("--");

}

System.out.println (Node.getname () + (Node.isleaf ()? "": "[" + Node.getchildren (). Size () + "]");

Set children = Node.getchildren ();

for (Iterator iter = Children.iterator (); Iter.hasnext ();) {

Node child = (node) iter.next ();

Printnode (child);

}

}

}

026 Hibernate operation tree structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.