Hibernate自关联

来源:互联网 发布:java 文件迁移 编辑:程序博客网 时间:2024/05/29 07:49
在部门中父部门和子部门有自关联关系:
public class Dept implements Serializable{private String id;private String deptName;//部门名字private Dept parent; //父部门,自关联private Integer state;//状态  0表示停用,1表示启用public String getId() {return id;}public void setId(String id) {this.id = id;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}public Dept getParent() {return parent;}public void setParent(Dept parent) {this.parent = parent;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}} 
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.jk.domain">   <class name="Dept" table="DEPT_P">      <id name="id" column="DEPT_ID">         <generator class="uuid"></generator>      </id>      <property name="deptName" column="DEPT_NAME"></property>      <property name="state" column="STATE"></property>           <!-- 自关联  子部门与父部门     多对一 -->      <many-to-one name="parent" class="Dept" column="PARENT_ID"></many-to-one>   </class></hibernate-mapping>