Hibernate---tree-like mapping

Source: Internet
Author: User

Head Office--branch 1, branch 2

Branch 1: Branch 1 Under Department 1, branch 1 under Department 2

Branch 2:

Org.java:

Package Com.bjsxt.hibernate;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; @Entitypublic class Org {private int id;private String name;private set<org> Children = new Hashset<org> ();p rivate 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 (Cascade=cascadetype.all, mappedby= "parent") public set<org> GetChildren () {return children;} public void Setchildren (set<org> children) {This.children = children;} @ManyToOne @joincolumn (name= "parent_id") public Org getParent () {return parent;} public void SetParent (Org parent) {this.parent = parent;}}

Hibernate.cfg.xml:

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate.                 Sourceforge.net/hibernate-configuration-3.0.dtd ">

Test:

Package Com.bjsxt.hibernate;import Java.util.map;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.tool.hbm2ddl.schemaexport;import Org.junit.afterclass;import Org.junit.beforeclass;import Org.junit.test;public class HibernateTreeTest {private Static sessionfactory sessionfactory; @BeforeClasspublic static void Beforeclass () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (false, true); sessionfactory = new Annotationconfiguration (). Configure (). Buildsessionfactory ();} @AfterClasspublic static void Afterclass () {sessionfactory.close ();} @Testpublic void Testsave () {Org o = new org (); O.setname ("head Office"); org O1 = new org (); O1.setname ("Branch 1"); Org o2 = new org (); O 2.setName ("Branch 2"), org o11 = new Org (), O11.setname ("branch 1 under Department 1"), org o12 = new Org (), O12.setname ("branch 1 under Department 2"), O. GetChildren (). Add (O1), O.getchildren (). Add (O2); O1.getchildren (). Add (o11); O1.getchildren (). Add (o12); o11. SetParent (O1); O12.setparenT (O1); O1.setparent (o); O2.setparent (o); Session session = Sessionfactory.opensession (); Session.begintransaction (); Session.save (o); Session.gettransaction ( ). commit (); Session.close ();} @Testpublic void TestLoad () {testsave (); Session session = Sessionfactory.opensession (); session.begintransaction (); org o = (org) session.load (org.class, 1); Print (o, 0); Session.gettransaction (). commit (); Session.close ();} private void print (Org o, int level) {String prestr = ' "; for (int i=0; i<level; i++) {prestr + = "----";} System.out.println (Prestr + o.getname ()); for (Org Child:o.getchildren ()) {print (child, level+1);}} @Testpublic void Testschemaexport () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (False, True) ;} public static void Main (string[] args) {beforeclass ();}}

  

  

Hibernate---tree-like mapping

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.