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