程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Hibernate三種映射關系(2)

Hibernate三種映射關系(2)

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved