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;
}
}
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;
}
}
映射文件:
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" />
<one-to-one name="teacher" class="com.entity.Teacher"
cascade="all" outer-join="auto" constrained="false" />
<!--
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>
<one-to-one name="student" class="com.entity.Student"
cascade="all" outer-join="auto" constrained="false" />
<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/99145