1
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; @BeforeClass public static void Beforeclass () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (FAL
SE, true);
Sessionfactory = new Annotationconfiguration (). Configure (). Buildsessionfactory ();
} @AfterClass public static void Afterclass () {sessionfactory.close ();
} @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 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 ();
} @Test public 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); }} @Test public void Testschemaexport () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (False,
true);
} public static void Main (string[] args) {beforeclass ();
}
}
2
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; @BeforeClass public static void Beforeclass () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (FAL
SE, true);
Sessionfactory = new Annotationconfiguration (). Configure (). Buildsessionfactory ();
} @AfterClass public static void Afterclass () {sessionfactory.close ();
} @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 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 ();
} @Test public 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); }} @Test public void Testschemaexport () {New Schemaexport (new Annotationconfiguration (). Configure ()). Create (False,
true);
} public static void Main (string[] args) {beforeclass ();
}
}