Hibernate inheritance Relationship Mapping relationship method (ii)--one table per subclass

Source: Internet
Author: User

TPS: The so-called "each subclass of a table (table Per Subclass)": The parent class is a table, each subclass a table, the parent class table holds the public information, the Child class table only saves its own unique information

This strategy uses the <joined-subclass> tag to define the subclass. The parent, subclass, and child classes correspond to a single database table. In the database table that corresponds to the parent class, it stores the public information for all the records, in effect the table that corresponds to the parent class contains all records, including the parent class and the child class records, and in the corresponding database table of the subclass, the table defines only the fields of the attribute mappings that are specific to the subclass.




Person table


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 Integer sid;   Student ID, connected to the parent person within the       private String Sno;    Study number       private String school;  School public       Integer GetSID () {return SID;} public void Setsid (Integer sid) {this.sid = SID;} 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 Integer wid;      Worker identification, connected to the parent person within the private String WNO;      Work number     private Double salary;   Payroll Public     Integer Getwid () {return wid;} public void Setwid (Integer wid) {this.wid = wid;} 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;}     
Map File Person.hbm.xml: <joined-subclass> tag needs to include a key tag, this tag specifies which field is connected between the child class and the parent class.

<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net /hibernate-mapping-3.0.dtd "> Database DAO 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 { After you save the student information and execute the segment code, the Id,name,sex,age of the student object is automatically saved in the person table, and the feature attribute Sid,sno,school is saved in the student table. The ID field of the person table and the SID field of the Student table are inside the connected 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); Hqlhibernate:select Student_ of the console print. Sid, student_1_. Name as name0_, student_1_. Sex as sex0_, student_1_. Age as age0_, Student_. Sno as Sno1_, Student_.      School as school1_ from student student_ inner JOIN person student_1_       On Student_. Sid=student_1_.id where Student_. Sid=? Hibernate:insert into who (Name, Sex, age, ID) VALUES (?,?,?,?) Hibernate:insert into student (Sno, School, Sid) VALUES (?,?,?) *///load student information, the process is that the parameter ID is connected to the person table, query the common field of the person table and the Student table's feature field 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 hql hibernate:select student0_. Sid as id0_0_, student0_1_. Name as name0_0_, student0_1_. Sex as sex0_0_, student0_1_. Age as age0_0_, student0_. Sno as sno1_0_, student0_. ScHool as school1_0_ from student student0_ INNER JOIN//Connect person with ID in person table student0_1_ On student0_. Sid=student0_1_.id where student0_.   Sid=? The parameter ID is the SID field of the student table *///The worker information is saved, the id,name,sex,age of the worker object is automatically saved in the person table when the segment code is executed, and the feature attribute wid,wno,salary is saved in the worker table. The ID field of the person table and the Wid field of the worker table are within the connected 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), Hqlhibernate:select worker_ for console printing. Wid, worker_1_. Name as name0_, worker_1_. Sex as sex0_, worker_1_. Age as age0_, Worker_. Wno as Wno2_, Worker_.  Salary as salary2_ from worker worker_ INNER join person worker_1_           On Worker_. Wid=worker_1_.id where Worker_. Wid=? Hibernate:insert into who (Name, Sex, age, ID) VALUES (?,?,?,?) Hibernate:insert into worker (WNO, Salary, Wid) VALUES (?,?,?) *///loading worker information, the process is that the parameter ID is connected to the person table, the public field of the person table is queried, and the feature field of the Woker table is common 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), HQL hibernate:select worker0_ for console printing. Wid as id0_0_, worker0_1_. Name as name0_0_, worker0_1_. Sex as sex0_0_, worker0_1_. Age as age0_0_, worker0_. Wno as wno2_0_, worker0_. Salary as salary2_0_ from worker worker0_ INNER JOIN//Connect with person in person table worker0_1_ on Wo rker0_. Wid=worker0_1_.id where worker0_. wid=?//parameter ID to join the Worker's table in the WID field */}
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" >Results:


HQL for console printing:

Hibernate:select Student_. Sid, student_1_. Name as name0_, student_1_. Sex as sex0_, student_1_. Age as age0_, Student_. Sno as Sno1_, Student_. School as school1_ from student student_ inner join person student_1_ on Student_. Sid=student_1_.id where Student_. Sid=? Hibernate:insert into who (Name, Sex, age, ID) VALUES (?,?,?,?) Hibernate:insert into student (Sno, School, Sid) VALUES (?,?,?) Hibernate:select student0_. Sid as id0_0_, student0_1_. Name as name0_0_, student0_1_. Sex as sex0_0_, student0_1_. Age as age0_0_, student0_. Sno as sno1_0_, student0_. School as school1_0_ from student student0_ inner join person student0_1_ on STUDENT0_.S Id=student0_1_.id where student0_. Sid=? Hibernate:select Worker_. Wid, worker_1_. Name As name0_, worker_1_. Sex as sex0_, worker_1_. Age as age0_, Worker_. Wno as Wno2_, Worker_. Salary as salary2_ from worker worker_ inner join person worker_1_ on Worker_. Wid=worker_1_.id where Worker_. Wid=? Hibernate:insert into who (Name, Sex, age, ID) VALUES (?,?,?,?) Hibernate:insert into worker (WNO, Salary, Wid) VALUES (?,?,?) Hibernate:select worker0_. Wid as id0_0_, worker0_1_. Name as name0_0_, worker0_1_. Sex as sex0_0_, worker0_1_. Age as age0_0_, worker0_. Wno as wno2_0_, worker0_. Salary as salary2_0_ from worker worker0_ inner join person worker0_1_ on worker0_. Wid=worker0_1_.id where worker0_. Wid=?























Hibernate inheritance Relationship Mapping relationship method (ii)--one table per subclass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.