JPA樹形結構實體關聯映射

來源:互聯網
上載者:User

  有時候我們需要設計樹形結構實體,比如常見的部門組織就是典型的樹形結構。

  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()方法,表中的資料為:

                        

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.