hibernate 自身多对多

有个类Student,它要自己对自己进行多对多映射

其中Student 类的属性有:

[java]  view plain copy
  1. public int id;  
  2. public String name;  
  3. public String sex;  
  4. private Set<Student> children = new HashSet<Student>();  
  5. private Set<Student> parent = new HashSet<Student>();  
自身多对多  可以拆成自身一对多

public class ParentS {  
  private Set<Student> children ;  
  
}    
public class Childs {  
  private Set<Student> parents;  
  
}  
下面是一个完成的自身一对多的列子

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>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值