Hibernate三种映射关系(2)
时间:2011-04-16 51cto博客 mengxiaohappy
one-to-one
假设有2张表student和teacher表,一个学生有一个老师,一个老师也只有一个学生。
类代码:
Teacher.java
package com.entity;
/**
* @hibernate.class table="teacher"
* @author Administrator
*
*/
public class Teacher {
private int id;
private String tname;
private Student student;
/**
* @hibernate.id generator-class="native"
* @return
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* @hibernate.one-to-one class="com.entity.Student" cascade="all"
* @return
*/
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
/**
* @hibernate.property column="tname"
* @return
*/
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
Hibernate三种映射关系(2)(2)
时间:2011-04-16 51cto博客 mengxiaohappy
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 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.one-to-one class="com.entity.Teacher" cascade="all"
* @return
*/
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
Hibernate三种映射关系(2)(3)
时间: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 in your merge dir.
-->
</generator>
</id>
|