hibernate继承关系映射方法(一)--共享一张表
生活随笔
收集整理的這篇文章主要介紹了
hibernate继承关系映射方法(一)--共享一张表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對于如下關系:
Person.java
package com.hust.PO;public class Person {private Integer id;private String name;private Integer age;private String sex;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}} Student.java
package com.hust.PO;public class Student extends Person {private String sno; //學號private String school; //學校public String getSno() {return sno;}public void setSno(String sno) {this.sno = sno;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;} } Worker.java
package com.hust.PO;public class Worker extends Person {private String wno; //工號private Double salary; //工資public String getWno() {return wno;}public void setWno(String wno) {this.wno = wno;}public Double getSalary() {return salary;}public void setSalary(Double salary) {this.salary = salary;}} hibernate實現此繼承關系映射時可以使用一張表:
字段“Type”是鑒別器,通過它區分student和worker對象實例
Person.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 package="com.hust.PO"><!-- 一個繼承樹一張表 --> <!-- 父類 --> <class name="Person" table="person"> <id column="id" name="Id" type="integer"> <!-- 主鍵生成策略為 '分配' --> <generator class="assigned"></generator> </id> <!-- 鑒定列,區分列 discriminator 鑒別器--> <discriminator column="Type" type="string"></discriminator><property column="Name" name="name" type="string"></property> <property column="Age" name="age" type="integer"></property> <property column="Sex" name="sex" type="string"></property> <!-- 子類,其鑒定列值為'stu' name是PO類名--> <subclass name="Student" discriminator-value="stu"> <property column="School" name="school" type="string"></property> <property column="Sno" name="sno" type="string"></property> </subclass> <!-- 子類,其鑒定列值為'worker' name是PO類名--> <subclass name="Worker" discriminator-value="worker"> <property column="Wno" name="wno" type="string"></property> <property column="Salary" name="salary" type="double"></property> </subclass> </class> </hibernate-mapping> 共享一張表時,映射文件要使用<subclass>標簽,當使用session保存或者修改子類對象時,會先檢查其子類的類型,然后對應subclass的的name屬性。
subclass標簽就是為子類嵌入父類的表中而設計的,而且要指定鑒別器,即用subclass一定要有判斷類型的一個列,鑒別器指定記錄屬于哪個類型
OneTableDao.java
package com.hust.Dao; import org.hibernate.Session; import org.hibernate.Transaction; import SessionFactory.HibernateSessionFactory; import com.hust.PO.Student; import com.hust.PO.Worker;public class OneTableDao {//保存學生信息,運行該段代碼后,程序會根據Student類設置person表中的Type值為“stu”,并把值插入到person表中,其他沒有的屬性都是空值public void saveStu(Student stu){Session session=HibernateSessionFactory.getSession();Transaction ts=null;try{ts=session.beginTransaction(); session.saveOrUpdate(stu);ts.commit();}catch(Exception ex){ts.rollback();System.out.println("添加學生信息失敗!");}finally{HibernateSessionFactory.closeSession();}}//獲取學生信息,會自動根據數據表字段“Type”的值,選擇記錄public Student loadStu(Integer id){Session session=HibernateSessionFactory.getSession();Transaction ts=null;Student stu=null;try{ts=session.beginTransaction();stu=(Student)session.get(Student.class, id);ts.commit();}catch(Exception ex){ts.rollback();System.out.println("獲取學生信息失敗!");}finally{HibernateSessionFactory.closeSession();}return stu;}//保存工人信息,,運行該段代碼后,程序會根據Worker類設置person表中的Type值為“worker”,并把值插入到person表中,其他沒有的屬性都是空值public void saveWorker(Worker worker){Session session=HibernateSessionFactory.getSession();Transaction ts=null;try{ts=session.beginTransaction(); session.saveOrUpdate(worker);ts.commit();}catch(Exception ex){ts.rollback();System.out.println("添加工人信息失敗!");}finally{HibernateSessionFactory.closeSession();}}//獲取工人信息public Worker loadWorker(Integer id){Session session=HibernateSessionFactory.getSession();Transaction ts=null;Worker worker=null;try{ts=session.beginTransaction();worker=(Worker)session.get(Worker.class, id);ts.commit();}catch(Exception ex){ts.rollback();System.out.println("獲取工人信息失敗!");}finally{HibernateSessionFactory.closeSession();}return worker;} } 測試頁面test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.hust.Dao.*" %> <%@ page import="com.hust.PO.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>hibernate的繼承關系一個繼承樹一張表</title><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h2>hibernate的繼承關系一個繼承樹一張表</h2><hr><%Student stu=new Student();stu.setId(new Integer(1));stu.setName("tuke");stu.setAge(new Integer(22));stu.setSex("nan");stu.setSno("M201571885");stu.setSchool("Hust");//保存stu對象OneTableDao dao=new OneTableDao();dao.saveStu(stu);//從數據庫中獲取stu對象Student stu1=dao.loadStu(stu.getId());out.println("<br>數據庫中的學生姓名:"+stu1.getName());out.println("<br>數據庫中的學生學號:"+stu1.getSno());out.println("<br>數據庫中的學生學校:"+stu1.getSchool());out.println("<br>");Worker worker=new Worker();worker.setId(new Integer(2));worker.setName("zhangsan");worker.setAge(new Integer(50));worker.setSex("nan");worker.setWno("W20152223");worker.setSalary(new Double(6409.32));//保存工人對象dao.saveWorker(worker);//從數據庫中獲取工人對象Worker worker2=dao.loadWorker(worker.getId());out.println("<br>數據庫中的工人姓名:"+worker2.getName());out.println("<br>數據庫中的工人工號:"+worker2.getWno());out.println("<br>數據庫中的工人工資:"+worker2.getSalary());%></body> </html>
測試結果:
控制臺打印的sql語句是:
Hibernate: selectstudent_.id,student_.Name as Name0_,student_.Age as Age0_,student_.Sex as Sex0_,student_.School as School0_,student_.Sno as Sno0_ fromperson student_ wherestudent_.id=? Hibernate: insert intoperson(Name, Age, Sex, School, Sno, Type, id) values(?, ?, ?, ?, ?, 'stu', ?) //插入stu執行的語句 Hibernate: selectstudent0_.id as id0_0_,student0_.Name as Name0_0_,student0_.Age as Age0_0_,student0_.Sex as Sex0_0_,student0_.School as School0_0_,student0_.Sno as Sno0_0_ fromperson student0_ wherestudent0_.id=? and student0_.Type='stu' // 加載stu信息時執行的語句 Hibernate: selectworker_.id,worker_.Name as Name0_,worker_.Age as Age0_,worker_.Sex as Sex0_,worker_.Wno as Wno0_,worker_.Salary as Salary0_ fromperson worker_ whereworker_.id=? Hibernate: insert intoperson(Name, Age, Sex, Wno, Salary, Type, id) values(?, ?, ?, ?, ?, 'worker', ?) //插入worker執行的語句 Hibernate: selectworker0_.id as id0_0_,worker0_.Name as Name0_0_,worker0_.Age as Age0_0_,worker0_.Sex as Sex0_0_,worker0_.Wno as Wno0_0_,worker0_.Salary as Salary0_0_ fromperson worker0_ whereworker0_.id=? and worker0_.Type='worker' //加載worker執行的語句
總結
以上是生活随笔為你收集整理的hibernate继承关系映射方法(一)--共享一张表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hibernate的一对多双向关联映射-
- 下一篇: mysql command line c