溫故知新:hibernate_10_表關聯_多對一雙向關聯(注解配置)

          和基于配置文件的多對一雙向關聯大體一致,測試類無需變動,撤銷XML的配置文件,將hibernate.cfg.xml中hbm的引入換成class的引入,如下
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <!DOCTYPE hibernate-configuration PUBLIC
           3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
           5 <hibernate-configuration>
           6     <session-factory name="sessionFactory">
           7         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
           8         <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_begin</property>
           9         <property name="hibernate.connection.username">root</property>
          10         <property name="hibernate.connection.password">root</property>
          11         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
          12         <property name="hibernate.hbm2ddl.auto">update</property>
          13         <property name="show_sql">true</property>
          14         <!-- <mapping resource="domain/Course.hbm.xml"/>
          15         <mapping resource="domain/Student.hbm.xml"/>
          16         <mapping resource="domain/StudentCourse.hbm.xml"/> -->
          17         <mapping class="domain.Student"/>
          18         <mapping class="domain.Teacher"/>
          19     </session-factory>
          20 </hibernate-configuration>
          21 
          同時為相關的實體類添加注解
          Student
           1 package domain;
           2 
           3 import javax.persistence.Entity;
           4 import javax.persistence.GeneratedValue;
           5 import javax.persistence.Id;
           6 import javax.persistence.JoinColumn;
           7 import javax.persistence.ManyToOne;
           8 import javax.persistence.Table;
           9 
          10 @Entity
          11 @Table(name="t_student")
          12 public class Student {
          13     
          14     private int id;
          15     private String name;
          16     private String grade;
          17     private Teacher teacher;
          18     
          19     public Student() {
          20         super();
          21     }
          22     
          23     public Student(String name, String grade, Teacher teacher) {
          24         super();
          25         this.name = name;
          26         this.grade = grade;
          27         this.teacher = teacher;
          28     }
          29     
          30     //設置自增主鍵
          31     @Id
          32     @GeneratedValue
          33     public int getId() {
          34         return id;
          35     }
          36     public void setId(int id) {
          37         this.id = id;
          38     }
          39     public String getName() {
          40         return name;
          41     }
          42     public void setName(String name) {
          43         this.name = name;
          44     }
          45     public String getGrade() {
          46         return grade;
          47     }
          48     public void setGrade(String grade) {
          49         this.grade = grade;
          50     }
          51     
          52     //為實體類添加映射和外鍵
          53     @ManyToOne
          54     @JoinColumn(name="tid")
          55     public Teacher getTeacher() {
          56         return teacher;
          57     }
          58     public void setTeacher(Teacher teacher) {
          59         this.teacher = teacher;
          60     }
          61     
          62 }
          63 

          Teacher
           1 package domain;
           2 
           3 import java.util.Set;
           4 
           5 import javax.persistence.Entity;
           6 import javax.persistence.GeneratedValue;
           7 import javax.persistence.Id;
           8 import javax.persistence.OneToMany;
           9 import javax.persistence.Table;
          10 
          11 import org.hibernate.annotations.LazyCollection;
          12 import org.hibernate.annotations.LazyCollectionOption;
          13 
          14 @Entity
          15 @Table(name="t_teacher")
          16 public class Teacher {
          17     
          18     private int id;
          19     private String name;
          20     private String course;
          21     private Set<Student> studs;
          22     
          23     public Teacher() {
          24         super();
          25     }
          26     
          27     public Teacher(String name, String course) {
          28         super();
          29         this.name = name;
          30         this.course = course;
          31     }
          32     
          33     @Id
          34     @GeneratedValue
          35     public int getId() {
          36         return id;
          37     }
          38     public void setId(int id) {
          39         this.id = id;
          40     }
          41     public String getName() {
          42         return name;
          43     }
          44     public void setName(String name) {
          45         this.name = name;
          46     }
          47     public String getCourse() {
          48         return course;
          49     }
          50     public void setCourse(String course) {
          51         this.course = course;
          52     }
          53 
          54     //mappedBy就意為將關系維護給到對方的teacher屬性,相當于inverse=true
          55     @OneToMany(mappedBy = "teacher")
          56     //LazyCollectionOption.EXTRA相當于lazy="extra"
          57     @LazyCollection(LazyCollectionOption.EXTRA)
          58     public Set<Student> getStuds() {
          59         return studs;
          60     }
          61 
          62     public void setStuds(Set<Student> studs) {
          63         this.studs = studs;
          64     }
          65 
          66 }
          67 

          posted on 2015-01-21 10:41 都較瘦 閱讀(185) 評論(0)  編輯  收藏 所屬分類: ORMFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 资溪县| 清河县| 文成县| 兖州市| 米易县| 呼伦贝尔市| 海宁市| 临颍县| 宜宾市| 独山县| 永昌县| 海原县| 红河县| 临洮县| 玛多县| 黔西| 广安市| 竹山县| 鄱阳县| 玉环县| 玉树县| 来宾市| 余江县| 科技| 黔西县| 榆树市| 思南县| 桐城市| 庄浪县| 和龙市| 蕉岭县| 克拉玛依市| 南开区| 江阴市| 岑巩县| 元阳县| 宾川县| 金川县| 宣汉县| 元谋县| 红原县|