The annotated version of Hibernate's tree retention method [more important XX] (22)

Source: Internet
Author: User
Tags commit

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 ();
 }
}



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.