Hibernate三种映射关系
时间:2011-04-16 51cto mengxiaohappy
many-to-many
一个学生有多个老师,一个老师也有多个学生。现在就在student和teacher这两张表的基 础上增加了一张关系表t_s,t_s为自动生成的表。
类代码:
Student.java
package com.entity;
import java.util.Set;
/**
* @hibernate.class table="student"
* @author Administrator
*
*/
public class Student {
private int id;
private String sname;
private Set teacherSet;
/**
* @hibernate.id generator-class="native"
* @return
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* @hibernate.property column="sname";
* @return
*/
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
/**
* @hibernate.set table="t_s" cascade="all" lazy="false" inverse="true"
* @hibernate.collection-key column="s_id"
* @hibernate.collection-many-to-many class="com.entity.Teacher" column="t_id"
* @return
*/
public Set getTeacherSet() {
return teacherSet;
}
public void setTeacherSet(Set teacherSet) {
this.teacherSet = teacherSet;
}
}
Hibernate三种映射关系(2)
时间:2011-04-16 51cto mengxiaohappy
Teacher.java
package com.entity;
import java.util.Set;
/**
* @hibernate.class table="teacher"
* @author Administrator
*
*/
public class Teacher {
private int id;
private String tname;
private Set studentSet;
/**
* @hibernate.id generator-class="native"
* @return
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* @hibernate.set table="t_s" cascade="all" lazy="false"
* @hibernate.collection-key column="t_id"
* @hibernate.collection-many-to-many class="com.entity.Student" column="s_id"
* @return
*/
public Set getStudentSet() {
return studentSet;
}
public void setStudentSet(Set studentSet) {
this.studentSet = studentSet;
}
/**
* @hibernate.property column="tname"
* @return
*/
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
映射文件:
Student.hbm.xml
<?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>
<class name="com.entity.Student" table="student">
<id name="id" column="id" type="int">
<generat
|