用hibernate對遞迴資料的操作

來源:互聯網
上載者:User
在工作中,我們常常會碰到具有遞迴性質記錄的資料,最常見的是某個機構部門節點的資料,某個節點一定會有個父節點屬性,同時它也許會有若干子節點。所有的節點資料都會存在資料庫中一張表中。這種現象在設計模式上叫Composite模式。下面我就給出一個用hibernate操作這種表的例子,關於hibernate配置和建立表結構的部分請參考我的前一篇文章《利用weblogic的資料來源作為hibernate的資料來源的例子》

1.持久化類Node.java
package com.jagie.business.organization;

/**
 * <p>Title: </p>
 * <p>Description: 部門節點</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: www.jagie.com</p>
 * @author Jagie
 * @version 1.0
 */

public class Node {
  private String ID;//pk
  private String name;//名稱
  private String description;//描述
  private Node parent;//上層業務
  private java.util.Set children;//下級部門
  public static void main(String[] args) {
  }
  public String getID() {
    return ID;
  }
  public void setID(String ID) {
    this.ID = ID;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Node getParent() {
    return parent;
  }
  public void setParent(Node parent) {
    this.parent = parent;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
  public String toString(){
    return name;
  }
  public java.util.Set getChildren() {
    return children;
  }
  public void setChildren(java.util.Set children) {
    this.children = children;
  }
}

2.對應檔Node.hbm.xml
[pre]<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
  <class name="com.jagie.business.organization.Node" table="SYS_Node">
    <id name="ID">
      <generator class="uuid.hex" />
    </id>
    <property name="name" />
    <property name="description" />
    <set cascade="save-update" inverse="true" lazy="true" name="children">
      <key column="parent" />
      <one-to-many class="com.jagie.business.organization.Node" />
    </set>
    <many-to-one cascade="none" class="com.jagie.business.organization.Node" 
      column="parent" name="parent" />
  </class>
</hibernate-mapping>[/pre]

3.用SchemaExport建立資料庫(略)

4.測試的代碼片斷

 //擷取net.sf.hibernate.Session和net.sf.hibernate.Transaction;
 Session s = JDOUtils.getInstance().getSessionFactory().openSession();
 Transaction t = s.beginTransaction();
 //建立父節點
 Node org = new Node();
 org.setName("北京總公司");
 org.setDescription("這是北京總公司");

 //建立子節點
 Node org2 = new Node();
 org2.setName("海澱分公司");
 org2.setDescription("這是海澱分公司");
 //建立子節點對父節點的引用
 org2.setParent(org);
    
 //持久化
 s.save(org);
 s.save(org2);

 t.commit();
 s.close();

代碼運行後,我們觀察資料庫,發現已經產生表SYS_Node,其結構為
id    varchar2(255)            
name    varchar2(255)    y        
description    varchar2(255)    y        
parent    varchar2(255)    y        

其中y的意思為nullable(可空),同時把我們的節點資料正確的插入了。
你可以自己再用session.find()方法來找出某父節點下面的所有子節點了。
最後,希望這篇短文對你有用。

聯繫我們

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