当前位置:首页 > 信息技术 > 软件项目开发 > Java软件开发 > 框架平台 > Hibernate > Hibernate一对多自身关联

Hibernate一对多自身关联

package com.zhaozi.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Category implements Serializable {

    private Integer id;
    private String name;
    private Category parent;
    private Set<Category> children = new HashSet<Category>();
   
    public Category() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    }

    public Set<Category> getChildren() {
        return children;
    }

    public void setChildren(Set<Category> children) {
        this.children = children;
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "../../../../hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.zhaozi.entity">
  <class name="Category" table="T_CATEGORY">
    <id name="id" column="CATEGORY_ID">
      <generator class="native"/>
    </id>
    <property name="name" type="string" column="NAME" length="15"/>
    <many-to-one name="parent" column="PARENT_ID" class="Category"/>
    <set name="children" cascade="save-update" inverse="true">
      <key column="PARENT_ID"/>
      <one-to-many class="Category"/>
    </set>
  </class>
</hibernate-mapping>

 

Hibernate一对多自身关联:等您坐沙发呢!

发表评论

表情
还能输入210个字