Hibernate三种映射关系

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张表studentteacher,一学生只有一个老师,一个老师有多个学生。
 
类代码:
 
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 >
 
 

你可能感兴趣的:(Hibernate,职场,学习笔记,休闲)