一学生只有一个老师,一个老师有多个学生。
类代码:
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;
}
}
Hibernate三种映射关系(5)
时间: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 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;
}
}
Hibernate三种映射关系(6)
时间:2011-04-16 51cto mengxiaohappy
映射文件:
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 i
|