hibernate註解形式的樹狀結構設計

package com.annotation.tree.dao;

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;
/*
 * 樹狀結構設計,在同一個類中同時使用@OneToMay與@MayToOne
 * 
 */
@Entity
public class TreeParentChild {

	private int id;
	private String name;
	private Set<TreeParentChild> children=new HashSet<TreeParentChild>();
	private TreeParentChild parent;
	
	@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;
	}
	
	@OneToMany(mappedBy="parent",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	public Set<TreeParentChild> getChildren() {
		return children;
	}
	public void setChildren(Set<TreeParentChild> children) {
		this.children = children;
	}
	
	@ManyToOne
	@JoinColumn(name="parent_id")
	public TreeParentChild getParent() {
		return parent;
	}
	public void setParent(TreeParentChild parent) {
		this.parent = parent;
	}
	
	
}

//測試類
package com.annotation.tree.dao;

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 Tree_pc_Test {

	public static SessionFactory sessionFactory;
	@BeforeClass
	public static void beforeClass()
	{
		try
		{
			new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); 
			sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
		}
		catch(Exception ex)
		{
			System.out.println("創建工廠異常");
		}
	}
	
	@Test
	public void createExport()
	{
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); 
	}
	
	@Test
	public void treeTest()
	{
		TreeParentChild tpc=new TreeParentChild();
		tpc.setName("總公司");
		TreeParentChild tpc1=new TreeParentChild();
		tpc1.setName("分公司1");
		TreeParentChild tpc2=new TreeParentChild();
		tpc2.setName("分公司2");
		TreeParentChild tpc11=new TreeParentChild();
		tpc11.setName("分公司1的部門1");
		TreeParentChild tpc21=new TreeParentChild();
		tpc21.setName("分公司2的部門2");
		
		tpc.getChildren().add(tpc1);     //設置它們之間的關係
		tpc.getChildren().add(tpc2);
		tpc1.getChildren().add(tpc11);
		tpc2.getChildren().add(tpc21);
		tpc11.setParent(tpc1);
		tpc21.setParent(tpc2);
		tpc1.setParent(tpc);
		tpc2.setParent(tpc);
		
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(tpc);
		session.getTransaction().commit();
	}
	
	@Test
	public void getTree()
	{
		treeTest();
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		TreeParentChild t=(TreeParentChild)session.get(TreeParentChild.class,1);
		print(t,0);
		session.getTransaction().commit();
	}
	
	private void print(TreeParentChild t, int level) {
		String str="";
		for(int i=0;i<level;i++)
			str+="----";
		System.out.println(str+t.getName());
		for(TreeParentChild child:t.getChildren())
			print(child,level+1);
			
		
	}

	@AfterClass
	public static void afterClass()
	{
		sessionFactory.close();
	}
	public static void main(String[] args)
	{
		beforeClass();
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章