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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值