Hibernate Tree-like mapping

Source: Internet
Author: User

Example:

A class Org represents an organization and is a typical tree-like structure of data whose properties include:

Id,name,children,parent

To map the org to a database, make a many-to-one mapping of the parent and a one-to-many mapping of the children.

We can explain the relationship by three sheets.

Code implementation:

1. Build Org entity class

@Entitypublic class Org {private int id;private String name;private set<org> childen = new hashset<org> ();p Riv Ate Org parent; @Id @generatedvaluepublic 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> Getchilden () { return childen;} public void Setchilden (set<org> childen) {This.childen = Childen;} @ManyToOnepublic Org getParent () {return parent;} public void SetParent (Org parent) {this.parent = parent;}}

2. Build JUnit Test class

@Testpublic void Testdelete () {New Schemaexport (new Configuration (). Configure ()). Create (True, true);

Run the program, automatically generate the table org in the database, there are 3 properties: id,name,parent_id (foreign key)

3. Storing data

@Testpublic void Testsave () {Session session = Sf.getcurrentsession (); session.begintransaction (); 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 o11 = new org (); o11.setname ("Branch 1_ Department 1"); org o12 = new org (); O12.setname ("Branch 1_ Department 2"); org o21 = new org (); O21.setname ("Branch 2_ Department 1"); O.getchilden (). Add (O1); O.getchilden (). Add (O2); O1.getchilden (). Add (o11); O1.getchilden (). Add (o12); O2.getchilden (). Add (o21); o11. SetParent (O1); o12.setparent (O1); O21.setparent (O2); O1.setparent (o); O2.setparent (o); Session.save (o); Session.gettransaction (). commit ();}

4. Print output

@Testpublic void TestLoad () {Session session = Sf.getcurrentsession (); session.begintransaction (); org o = (org) Session.load (Org.class, 1);p rint (o,0); Session.gettransaction (). commit ();} The name of the private void print (Org o,int level) {for (int i=0;i<level;i++) {System.out.print ("----") of each object is printed and exported recursively, in a tree-like structure;} System.out.println (O.getname ()); for (Org Child:o.getchilden ()) {print (child,level+1);}}
Output results
Head Office ---- Branch 1-------- Branch 1_ Department 2-------- Branch 1_ Department 1---- Branch 2--------Branch 2_ Department 1

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.