论坛首页 Java企业应用论坛

面向对象的树型结构设计与实现

浏览 2386 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (8)
作者 正文
   发表时间:2009-08-24  
OO
面向对象树型结构的设计与实现,以机构为例,机构有它的上级机构和下级机构,以下是相关代码的实现:

Orgnization.java
package com.changtu.oa.model;

import java.util.Set;

/**
 * 组织机构(树型结构)
 * @author Mike
 *
 */
public class Orgnization {

	private int id;
	/**
	 * 机构名称
	 */
	private String name;
	/**
	 * 
	 */
	private String sn;
	/**
	 * 机构描述
	 */
	private String description;
	
	/**
	 * 树型结构,一对多的双向关联
	 */
	private Orgnization parent;
	private Set children;
	
	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;
	}
	public String getSn() {
		return sn;
	}
	public void setSn(String sn) {
		this.sn = sn;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Orgnization getParent() {
		return parent;
	}
	public void setParent(Orgnization parent) {
		this.parent = parent;
	}
	public Set getChildren() {
		return children;
	}
	public void setChildren(Set children) {
		this.children = children;
	}
}


hibernate的映射配置:Orgnization.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class table="T_Orgnization" name="com.changtu.oa.model.Orgnization">
		<id name="id">
			<generator class="identity" />
		</id>
		<property name="name"></property>
		<property name="sn"></property>
		<property name="description"></property>
		
		<!-- 树型结构,一对多双向关联 -->
		<many-to-one name="parent" column="pid" lazy="false"></many-to-one>
		<set name="children" inverse="true" cascade="all">
			<key column="pid"></key>
			<one-to-many class="com.changtu.oa.model.Orgnization" />
		</set> 
		
	</class>

</hibernate-mapping>


   以上是实体以及hibernate配置文件的编写,具体服务层接口的定义与实现和呈现层的实现可以根据的需求去给予实现。
   发表时间:2009-09-08  
貌似自连接。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics