樹狀結構的設計(把一棵樹存入資料庫的hibernate映射,及資料庫表的設計)__資料庫

來源:互聯網
上載者:User

資料庫模型(id-pid):一個父親有多個孩子,而一個孩子只有一個父親,這樣的結構就是一顆樹。在資料庫表中,設計的是id和pid(parent_id)。通過id和pid就存放一顆樹了。

從物件導向的設計角度出發,在樹的節點類中不宜放pid,一個節點有1個或者0個父親,而有0個或者孩子。因此設計如下:這是一個組織機構,每個機構有父機構和子機構。

package com.xie.hibernate.modal;

 

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;

@Entity

public class Org {

   private int id;

   private String name;

   private Set<Org> childrenOrgs=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> getChildrenOrgs() {

    return childrenOrgs;

}

public void setChildrenOrgs(Set<Org> childrenOrgs) {

    this.childrenOrgs = childrenOrgs;

}

@ManyToOne

@JoinColumn(name="p_id")

public Org getParent() {

    return parent;

}

public void setParent(Org parent) {

    this.parent = parent;

} 

}

下面是junit的測試方法:

//產生資料庫表,同時插入資料:

    @Test

    public void testSave(){

       Org o=new Org();

       o.setName("總公司");

       Org o1=new Org();

       o1.setName("分公司1");

       Org o2=new Org();

       o2.setName("分公司2");

       Org o3=new Org();

       o3.setName("分公司1的分公司1");

       Org o4=new Org();   

       o4.setName("分公司1的分公司2");

       Org o5=new Org();   

       o5.setName("分公司2的分公司1");

       Org o6=new Org();   

       o6.setName("分公司1的分公司1分公司1");

      

       o1.setParent(o);

       o2.setParent(o);

       o3.setParent(o1);

       o4.setParent(o1);

       o5.setParent(o2);

       o6.setParent(o3);

       o.getChildrenOrgs().add(o1);

       o.getChildrenOrgs().add(o2);

       o1.getChildrenOrgs().add(o3);

       o1.getChildrenOrgs().add(o4);

       o2.getChildrenOrgs().add(o5);

       o3.getChildrenOrgs().add(o6);

       try {

           Session session=sf.getCurrentSession();

           session.beginTransaction();

            //註解中加了cascade={CascadeType.ALL},只存跟節點就把整顆樹

            

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.