TPC: The so-called "a table per concrete class" means that each sub-class in the inheritance system corresponds to a table in the database . each child class corresponds to a database table that contains information about the parent class and contains its own unique properties. each subclass corresponds to a table, and the information for this table is complete, which is the field that contains all attribute mappings inherited from the parent class. This strategy uses the <union-subclass> tag to define the subclass.
Note: Three classes + one parent class mapping file + two tables
Student table
Worker table
Test Engineering:
Person.java
Package Com.hust.po;public class Person { private Integer ID; private String name; Name private Integer age; Age private String sex; Gender 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; Study number private String school; 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; Work number private Double salary; Payroll 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;}
Parent class Mapping File Person.hbm.xml
<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net /hibernate-mapping-3.0.dtd "> The <union-subclass> tag is a subclass that indicates the class represented by the hbm file, such as two subclasses of the person class, which requires two <union-subclass> tags and so on.The "name" property of the <union-subclass> tag is used to specify the fully qualified name of the subclass, and the "table" attribute is used to specify the name of the table corresponding to the subclass, and the "extends" property is used to specify the parent class of the subclass.
Note:the "extends" property is related to the location of the <union-subclass> label, and if the <union-subclass> tag is a sub-label of the <class> label, the Extends "property can not be set, otherwise you need to explicitly set the" Extends "property.
The "abstract" property in the <class> tag if the value is true, the table structure is not generated. A value of false results in a table structure, but no data is inserted.
Database File Tabledao.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 TableDao { Save student Information 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 ("Add student information failed"); }finally{hibernatesessionfactory.closesession (); }}/*session.saveorupdate (STU); console-printed HQL * Hibernate:select Student_. Id, Student_. Name as name0_, Student_. Sex as sex0_, Student_. Age as age0_, Student_. Sno as Sno1_, Student_. School as school1_ from student Student_ where Student_. Id=? Hibernate:insert into student (Name, Sex, age, Sno, School, Id) VALUES (?,?,?,?,?,? ) *///Load Student LetterInterest 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 ("Load Student information failed"); }finally{hibernatesessionfactory.closesession (); } return Stu; }/* stu= (Student) session.get (Student.class, id), and the console prints Hqlhibernate:select student0_. Id as id0_0_, student0_. Name as name0_0_, student0_. Sex as sex0_0_, student0_. Age as age0_0_, student0_. Sno as sno1_0_, student0_. School as school1_0_ from student student0_ where student0_. id=?*///Save worker Information 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 ("Add worker information failed"); }finally{hibernatesessionfactory.closesession (); }}/*session.saveorupdate (worker), HQL * hibernate:select worker_ for console printing. Id, Worker_. Name as name0_, Worker_. Sex as sex0_, Worker_. Age as age0_, Worker_. Wno as Wno2_, Worker_. Salary as salary2_ from worker Worker_ where Worker_. Id=? Hibernate:insert into worker (Name, Sex, age, WNO, Salary, Id) VALUES (?,?,?,?,?,?) *///loading worker Information 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 ("Loading worker information failed"); }finally{hibernatesessionfactory.closesession (); } return worker; }/*worker= (worker) Session.get (Worker.class, id), console-printed hql * * Hibernate:select worker0_. Id as id0_0_, worker0_. Name as name0_0_, worker0_. Sex as sex0_0_, worker0_. Age as age0_0_, worker0_. Wno as wno2_0_, worker0_. Salary as salary2_0_ from worker worker0_ where worker0_. id=?*/}
Test Page 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" >Test results:
Student the value of the table insert:
Values inserted by the worker table:
HQL of the console print shop
Hibernate:select Student_. Id, Student_. Name as name0_, Student_. Sex as sex0_, Student_. Age as age0_, Student_. Sno as Sno1_, Student_. School as school1_ from student Student_ where Student_. Id=? Hibernate:insert into student (Name, Sex, age, Sno, School, Id) VALUES (?,?,?,?,?,? ) Hibernate:select student0_. Id as id0_0_, student0_. Name as name0_0_, student0_. Sex as sex0_0_, student0_. Age as age0_0_, student0_. Sno as sno1_0_, student0_. School as school1_0_ from student student0_ where student0_. Id=? Hibernate:select Worker_. Id, Worker_. Name as name0_, Worker_. Sex as sex0_, Worker_. Age as age0_, Worker_. Wno as Wno2_, Worker_. Salary as salary2_ from worker Worker_ where Worker_. Id=? Hibernate:insert into worker (Name, Sex, age, WNO, Salary, Id) VALUES (?,?,?,?,?,?) Hibernate:select worker0_. Id as id0_0_, worker0_. Name as name0_0_, worker0_. Sex as sex0_0_, worker0_. Age as age0_0_, worker0_. Wno as wno2_0_, worker0_. Salary as salary2_0_ from worker worker0_ where worker0_. Id=?
Hibernate inheritance Relationship mapping Method (iii)--one table per specific class TPC