hibernate注解形式的树状结构设计
2012-09-29 10:57
441 查看
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;@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();}}
相关文章推荐
- 树状结构的设计(把一棵树存入数据库的hibernate映射,及数据库表的设计)
- Hibernate之树状结构设计
- 【hibernate框架】练习-树状结构设计(非常重要)
- Hibernate学习实例:关联表的树状结构设计
- Hibernate 另一个作业:Org树状结构设计
- hibernate实体层次设计之三种类型的继承形式(三)
- 增强版JAVA实现文本形式的树状结构显示
- 用JAVA实现文本形式的树状结构显示(转)
- 树状结构表设计
- 用JAVA实现文本形式的树状结构显示
- hibernate注解反向生成表结构
- 用java实现文本形式的树状结构显示
- Hibernate基础之十一:树状结构遍历
- 【GOF23设计模式】_组合模式_树状结构_杀毒软件架构_JUnite底层架构JAVA239
- 设计及封装无限层级的树状结构
- hibernate教程____hibernate时启动创建表结构(使用实体类注解不用hbm.xml)
- 【SqlServer-无限递归树状图结构设计和查询】
- hibernate笔记-016-树状结构
- Hibernate4学习总结(4)--注解形式的集合映射,关联关系映射
- hibernate JPA 单表树形结构 注解配置