• Hibernate 树状映射


    示例:

    类Org表示组织机构,是一个典型的树状结构数据,其属性包括:

    id,name,children,parent

    要将Org映射到数据库中,对parent作多对一的映射,对children作一对多的映射。

    我们可以通过三张表来解释其关系

    1

    代码实现:

    1.建Org实体类

    @Entity
    public class Org {
    	private int id;
    	private String name;
    	private Set<Org> childen = new HashSet<Org>();
    	private Org 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<Org> getChilden() {
    		return childen;
    	}
    	public void setChilden(Set<Org> childen) {
    		this.childen = childen;
    	}
    	@ManyToOne
    	public Org getParent() {
    		return parent;
    	}
    	public void setParent(Org parent) {
    		this.parent = parent;
    	}
    }

    2.建Junit测试类

    @Test
    public void testDelete() {
    	new SchemaExport(new Configuration().configure()).create(true, true);
    			
    }

    运行程序,在数据库中自动生成表org,有3个属性:id,name,parent_id(外键)

    3.存储数据

    @Test
    public void testSave() {
    		
    	Session session = sf.getCurrentSession();
    	session.beginTransaction();
    		
    	Org o = new Org();
    	o.setName("总公司");
    		
    	Org o1 = new Org();
    	o1.setName("分公司1");
    		
    	Org o2 = new Org();
    	o2.setName("分公司2");
    		
    	Org o11 = new Org();
    	o11.setName("分公司1_部门1");
    		
    	Org o12 = new Org();
    	o12.setName("分公司1_部门2");
    		
    	Org o21 = new Org();
    	o21.setName("分公司2_部门1");
    		
    	o.getChilden().add(o1);
    	o.getChilden().add(o2);
    	o1.getChilden().add(o11);
    	o1.getChilden().add(o12);
    	o2.getChilden().add(o21);
    		
    		
    	o11.setParent(o1);
    	o12.setParent(o1);
    	o21.setParent(o2);
    	o1.setParent(o);
    	o2.setParent(o);
    		
    	session.save(o);
    
    	session.getTransaction().commit();		
    }

    4.打印输出

    @Test
    public void testLoad() {
    	
    	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) {
    	for(int i=0;i<level;i++){
    		System.out.print("----");
    	}
    	System.out.println(o.getName());
    	for(Org child : o.getChilden()){
    		print(child,level+1);
    	}
    }
    输出结果
    总公司
    ----分公司1
    --------分公司1_部门2
    --------分公司1_部门1
    ----分公司2
    --------分公司2_部门1
     
  • 相关阅读:
    art-template学习(一)之特性和安装
    Velocity学习(四)之判断是否为空以及为什么要使用{}来明确标识变量
    Velocity学习(三)之 ${} 和$!{}、!${}区别
    sqlcode、sqlerrm
    批量删除存储过程
    cursor 与refcursor及sys_refcursor的区别 (转载)
    分享最近写的几个存储过程
    中国人寿数据库死锁处理
    合理计划 dictionary cache 大小
    表空间、数据文件对应关系
  • 原文地址:https://www.cnblogs.com/weilunhui/p/3905765.html
Copyright © 2020-2023  润新知