有个类Student,它要自己对自己进行多对多映射
其中Student 类的属性有:
- public int id;
- public String name;
- public String sex;
- private Set<Student> children = new HashSet<Student>();
- 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>