Hibernate-tree ing and hibernate tree

Source: Internet
Author: User

Hibernate-tree ing and hibernate tree

Tree Map

Hypothesis:

The structure of a table is as follows:

Id

Parent_id

Name

 

 

 

The company's organizational structure is tree-like ing

Example:

Package com. zgy. hibernate. model;

 

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. onetoence;

 

 

/*

* Org class, indicating the class of the Organizational Structure

*/

@ Entity

Public class Org {

Private int id; // number

Private String name; // name

Private Set <Org> children = new HashSet <Org> (); // subclass of Org

Private Org parent; // The parent class of Org

@ Id

@ GeneratedValue

Public 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;

}

@ Onetoager (cascade = CascadeType. ALL, mappedBy = "parent", fetch = FetchType. EAGER)

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;

}

}

 

 

Test:

Package com. zgy. hibernate. model;

 

Import java. util. Date;

Import java. util. List;

 

Import org. hibernate. Query;

 

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 sf;

@ BeforeClass

Public static void beforeClass (){

Sf = new AnnotationConfiguration (). configure (). buildSessionFactory ();

}

@ AfterClass

Public static void afterClass (){

Sf. close ();

}

@ Test

Public void testSchemaExport (){

New SchemaExport (new AnnotationConfiguration (). configure (). create (false, true );

}

@ 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 ("department 1 under branch 1 ");

Org o12 = new Org ();

O12.setName ("branch 1 Department 2 ");

O. getChildren (). add (o1 );

O. getChildren (). add (o2 );

O1.getChildren (). add (o11 );

O2.getChildren (). add (o12 );

O11.setParent (o1 );

O12.setParent (o1 );

O1.setParent (o );

O2.setParent (o );

Session session = sf. openSession ();

Session. beginTransaction ();

Session. save (o );

Session. getTransaction (). commit ();

Session. close ();

}

@ Test

Public void testLoad (){

TestSave ();

Session session = sf. getCurrentSession ();

Session. beginTransaction ();

Org o = (Org) session. load (Org. class, 1 );

Print (o, 0 );

Session. getTransaction (). commit ();

}

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

}

}

Public static void main (String [] args ){

BeforeClass ();

}

}




Related Article

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.