Topic: Hibernate Spanning Tree (annotation-based approach)

Source: Internet
Author: User
Tags commit

Java Code

Package Yingjun.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.OneToMany;
	@Entity public class Tree {private int id;
	private String name;
	Private Tree parent;
	Private set<tree> children=new hashset<tree> ();
	@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;
	} @ManyToOne @JoinColumn (name= "parent_id") public Tree getParent () {return parent;
	} public void SetParent (Tree parent) {this.parent = parent; } @OneToMany (mappedby= "parent", Cascade=cascadetype.all,fetch=fetchtype.eager) public set<tree> GetChildren () {REturn children;
	} public void Setchildren (set<tree> children) {This.children = children;
 }

	
}

Generated statement of the build table:

CREATE TABLE Tree (
        ID integer NOT NULL auto_increment,
        name varchar (255),
        parent_id Integer,
        Primary Key (ID)
    )

    ALTER TABLE Tree 
        add index fk27e7be9fd80f87 (parent_id), 
        add constraint fk27e7be9fd80f87 
        foreign Key (parent_id) 
        references Tree (ID)

Inserting data into the tree

@Test public
	void Testsavetree () {
		
		Session session=hibernateutil.getsessionfactory (). Getcurrentsession ();
		Session.begintransaction ();
		Tree tree1=new tree ();
		Tree1.setname ("picture");
		
		Tree tree2=new tree ();
		Tree2.setname ("photo");
		
		Tree tree3=new tree ();
		Tree3.setname ("poster");
		
		Tree tree4=new tree ();
		Tree4.setname ("My photo 1");
		
		Tree tree5=new tree ();
		Tree5.setname ("My photo 2");
		
		Tree1.getchildren (). Add (tree2);
		Tree1.getchildren (). Add (tree3);
		Tree2.getchildren (). Add (tree4);
		Tree2.getchildren (). Add (tree5);
		Tree2.setparent (tree1);
		Tree3.setparent (tree1);
		Tree4.setparent (tree2);
		Tree5.setparent (tree2);	
		
		Session.save (tree1);
		Session.gettransaction (). commit ();
	}

Results:


To fetch the data code from the tree:

@Test public
	void Testloadtree () {
		Session session=hibernateutil.getsessionfactory (). Getcurrentsession ();
		Session.begintransaction ();
		Tree t= (tree) session.load (Tree.class, 1);
		Print (t);
		Session.gettransaction (). commit ();
		
		
	}

	private void print (tree tree) {
		System.out.println (Tree.getname ());
		For (Tree Child:tree.getChildren ()) {
			print (child);
		}

To remove the result:


Reprint Source:

http://www.iteye.com/topic/1129579

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.