有时候我们需要设计树形结构实体,比如常见的部门组织就是典型的树形结构。
1、组织架构实体代码:
package cn.luxh.jpa.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; 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; import javax.persistence.Table; /** * 组织架构 * @author Luxh */ @Entity @Table(name="t_organization") public class Organization { @Id @GeneratedValue private Long id; /**组织名称*/ @Column(length=64) private String name; /**组织编码*/ @Column(length=64) private String code; /**父组织*/ @ManyToOne @JoinColumn(name="parent_id") private Organization parent; /**子组织*/ @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY) @JoinColumn(name="parent_id") private Set<Organization> children = new HashSet<Organization>(); //省略get/set方法 //... }
JPA生成的对应表结构为:
2、单元测试代码:
package cn.luxh.jpa.test; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.luxh.jpa.entity.Organization; public class JPATest { EntityManagerFactory emf = null; @Before public void before() { //根据在persistence.xml中配置的persistence-unit name 创建EntityManagerFactory emf = Persistence.createEntityManagerFactory("myJPA"); } /** * 保存父组织,级联保存子组织 */ @Test public void testAddParentOrg() { //父组织 Organization chinaOrg = new Organization(); chinaOrg.setName("中国"); chinaOrg.setCode("CHINA"); //子组织 Organization gdOrg = new Organization(); gdOrg.setName("广东"); gdOrg.setCode("GD"); //子组织 Organization gxOrg = new Organization(); gxOrg.setName("广西"); gxOrg.setCode("GX"); Set<Organization> children = new HashSet<Organization>(); children.add(gdOrg); children.add(gxOrg); //添加子组织 chinaOrg.setChildren(children); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.persist(chinaOrg); em.getTransaction().commit(); em.close(); } /** * 保存子组织 */ @Test public void testAddChildOrg() { Organization gzOrg = new Organization(); gzOrg.setName("广州"); gzOrg.setCode("GZ"); EntityManager em = emf.createEntityManager(); //找出广州所属的父组织广东 Organization parent = em.find(Organization.class, 3L); //设置广州的父组织 gzOrg.setParent(parent); em.getTransaction().begin(); //保存广州 em.persist(gzOrg); em.getTransaction().commit(); em.close(); } /** * 根据子组织查询父组织 */ @Test public void testQueryParentByChild() { EntityManager em = emf.createEntityManager(); //找出广州 Organization gzOrg = em.find(Organization.class, 4L); //找出父组织 Organization parent = gzOrg.getParent(); System.out.println("父组织名称:"+parent.getName()); em.close(); } /** * 根据父组织查询子组织 */ @Test public void testQueryChildrenByParent() { EntityManager em = emf.createEntityManager(); //找出广东 Organization gdOrg = em.find(Organization.class, 3L); //找出子组织 Set<Organization> children = gdOrg.getChildren(); Iterator<Organization> it = children.iterator(); while(it.hasNext()) { Organization child = it.next(); System.out.println("子组织名称:"+child.getName()); } em.close(); } /** * 关闭EntityManagerFactory */ @After public void after() { if(null != emf) { emf.close(); } } }
1)执行完testAddParentOrg()方法,表中的数据为:
2)执行完testAddChildOrg()方法,表中的数据为: