Design of tree structure (hibernate mapping of a tree to database and design of database tables) __ Database

Source: Internet
Author: User

Database model (ID-PID): A father has many children, and a child has only one father, such a structure is a tree. In a database table, the ID and PID (parent_id) are designed. A tree is stored by ID and PID.

From the object-oriented design point of view, in the tree node class is not suitable for PID, a node has 1 or 0 fathers, and 0 or children. So the design is as follows: This is an organizational structure, each institution has its parent and child agencies.

Package com.xie.hibernate.modal;

Import Java.util.HashSet;

Import Java.util.Set;

Import Javax.persistence.CascadeType;

Import javax.persistence.Entity;

Import Javax.persistence.FetchType;

Import Javax.persistence.GeneratedValue;

Import Javax.persistence.Id;

Import Javax.persistence.JoinColumn;

Import Javax.persistence.ManyToOne;

Import Javax.persistence.OneToMany;

@Entity

public class Org {

private int id;

private String name;

Private set<org> childrenorgs=new hashset<org> ();

Private ORG parent;

@Id

@GeneratedValue

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;

}

@OneToMany (mappedby= "parent", Cascade={cascadetype.all},fetch=fetchtype.eager)

Public set<org> Getchildrenorgs () {

return Childrenorgs;

}

public void Setchildrenorgs (set<org> Childrenorgs) {

This.childrenorgs = Childrenorgs;

}

@ManyToOne

@JoinColumn (name= "p_id")

Public Org GetParent () {

return to parent;

}

public void SetParent (ORG parent) {

This.parent = parent;

}

}

The following are the JUnit test methods:

To generate a database table and insert data at the same time:

@Test

Public void Testsave () {

ORG o=new org ();

O.setname ("head Office");

ORG o1=new org ();

O1.setname ("branch 1");

ORG o2=new org ();

O2.setname ("branch 2");

ORG o3=new org ();

O3.setname ("Branch 1 of Branch 1");

ORG o4=new org ();

O4.setname ("Branch 1 of Branch 2");

ORG o5=new org ();

O5.setname ("Branch 2 of Branch 1");

ORG o6=new org ();

O6.setname ("Branch 1 branch 1 branch 1");

O1.setparent (o);

O2.setparent (o);

O3.setparent (O1);

O4.setparent (O1);

O5.setparent (O2);

O6.setparent (O3);

O.getchildrenorgs (). Add (O1);

O.getchildrenorgs (). Add (O2);

O1.getchildrenorgs (). Add (O3);

O1.getchildrenorgs (). Add (O4);

O2.getchildrenorgs (). Add (O5);

O3.getchildrenorgs (). Add (O6);

Try {

Session session=sf.getcurrentsession ();

Session.begintransaction ();

Note added Cascade={cascadetype.all}, only save the node with the whole tree

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.