hibernate注解形式的树状结构设计

来源:互联网 发布:php输出素数 编辑:程序博客网 时间:2024/05/18 15:50
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 *  */@Entitypublic class TreeParentChild {private int id;private String name;private Set<TreeParentChild> children=new HashSet<TreeParentChild>();private TreeParentChild parent;@Id@GeneratedValuepublic 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;@BeforeClasspublic static void beforeClass(){try{new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();}catch(Exception ex){System.out.println("创建工厂异常");}}@Testpublic void createExport(){new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }@Testpublic 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();}@Testpublic 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);}@AfterClasspublic static void afterClass(){sessionFactory.close();}public static void main(String[] args){beforeClass();}}



原创粉丝点击