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;
}
}
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">
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Student.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property name="sname" type="java.lang.String" update="true"
insert="true" column="sname" />
<set name="teacherSet" table="t_s" lazy="false" inverse="true"
cascade="all" sort="unsorted">
<key column="s_id"></key>
<many-to-many class="com.entity.Teacher" column="t_id"
outer-join="auto" />
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Student.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Teacher.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.Teacher" table="teacher">
<id name="id" column="id" type="int">
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Teacher.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set name="studentSet" table="t_s" lazy="false" cascade="all"
sort="unsorted">
<key column="t_id"></key>
<many-to-many class="com.entity.Student" column="s_id"
outer-join="auto" />
</set>
<property name="tname" type="java.lang.String" update="true"
insert="true" column="tname" />
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Teacher.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
one-to-many
假設有2張表student和teacher,一學生只有一個老師,一個老師有多個學生。
類代碼:
Student.java
package com.entity;
/**
* @hibernate.class table="student"
* @author Administrator
*
*/
public class Student {
private int id;
private String sname;
private Teacher teacher;
/**
* @hibernate.id column="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.many-to-one class="com.entity.Teacher"
* column="t_id"
* cascade="all"
* @return
*/
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
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 students;
/**
* @hibernate.id column="id" generator-class="native"
* @return
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* @hibernate.set cascade="all" inverse="true" lazy="false"
* @hibernate.collection-key column="t_id"
* @hibernate.collection-one-to-many class="com.entity.Student"
* @return
*/
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
/**
* @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">
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Student.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property name="sname" type="java.lang.String" update="true"
insert="true" column="sname" />
<many-to-one name="teacher" class="com.entity.Teacher"
cascade="all" outer-join="auto" update="true" insert="true"
column="t_id" />
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Student.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Teacher.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.Teacher" table="teacher">
<id name="id" column="id" type="int">
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Teacher.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set name="students" lazy="false" inverse="true" cascade="all"
sort="unsorted">
<key column="t_id"></key>
<one-to-many class="com.entity.Student" />
</set>
<property name="tname" type="java.lang.String" update="true"
insert="true" column="tname" />
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Teacher.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
本文出自 “夢潇” 博客,請務必保留此出處 http://grape.blog.51cto.com/293764/99138