Hibernate inherited link ing method (2)-a table for each subclass

Source: Internet
Author: User

Hibernate inherited link ing method (2)-a table for each subclass

TPS: "One Table Per sub-class (Table Per Subclass )":The parent class is a table, and each sub-class is a table. The parent class Table stores public information. The sub-class table only saves its own unique information.

This policy is used Label to define the subclass. The parent class and Child class correspond to a database table. In the database table corresponding to the parent class, it stores the public information of all records. In fact, the table corresponding to the parent class contains all records, including records of the parent class and subclass; in the database table corresponding to the subclass, this table only defines the specific attribute ing fields in the subclass.

 

Person table

Student table

Worker table

Test Project:

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, which is connected to the private String sno in the parent class person; // Student ID 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 ID, which is connected to the private String wno in the parent class person; // employee ID private Double salary; // salary 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 ;}}
Person ing file Person. hbm. xml: The tag must contain a key tag. This label specifies the field in which the child class and parent class are connected.

 

 

     
       
        
           
           
                 
                  
              
             
             
             
                       
             
                 
      
                
     
                
        
            
                       
             
                 
                  
      
                
      
            
         
    
   
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 {// Save the student information. After the code segment is executed, the id, name, sex, and age of the student object will be automatically saved in the person table, feature attribute sid, sno, school will save it in the student table. The Id field of the person table and the Sid field of the student table are public void saveStu (Student stu) {Session session = HibernateSessionFactory. g EtSession (); Transaction ts = null; try {ts = session. beginTransaction (); session. saveOrUpdate (stu); ts. commit ();} catch (Exception ex) {ts. rollback (); System. out. println ("failed to add student information");} finally {HibernateSessionFactory. closeSession () ;}}/* session. saveOrUpdate (stu); HQLHibernate printed on the console: 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 _ inner join person student_1 _ on student _. Sid = student_1 _. id where student _. Sid =? Hibernate: insert into person (Name, Sex, Age, id) values (?, ?, ?, ?) Hibernate: insert into student (Sno, School, Sid) values (?, ?, ?) * /// Load student information. The process is to connect the parameter id to the person table, and query the public field of the person table and the feature field public Student loadStu (Integer id) of the student table) {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 ("failed to load student information");} finally {HibernateSessionFactory. closeSession ();} Return stu;}/* stu = (Student) session. get (Student. class, id); HQL Hibernate printed on the console: 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 snostm0 _, student0 _. school as School1_0 _ from student student0 _ inner join // connect to the person student0_1 _ on student0 _ in the Id of the person table _. sid = student0_1 _. id where student0 _. sid =? // The parameter id is the Sid field of the student table * // Save the worker information. After the code segment is executed, the id, name, sex, and age of the worker object are automatically saved in the person table, the wid, wno, and salary feature attributes are stored in the worker table. The Id field of the person table and the Wid field of the worker table are the public void saveWorker (Worker worker) connected internally) {Session session = HibernateSessionFactory. getSession (); Transaction ts = null; try {ts = session. beginTransaction (); session. saveOrUpdate (worker); ts. commit ();} catch (Exception ex) {ts. rollback (); System. out. println ("failed to add worker information" );} Finally {HibernateSessionFactory. closeSession () ;}}/* session. saveOrUpdate (worker); HQLHibernate printed on the console: 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 _ inner join person worker_1 _ on worker _. wid = worker_1 _. id where worker _. wid =? Hibernate: insert into person (Name, Sex, Age, id) values (?, ?, ?, ?) Hibernate: insert into worker (Wno, Salary, Wid) values (?, ?, ?) * // Load the Worker information. The process is to connect the parameter id to the person table, and query the public fields of the person table and the feature fields of the woker table 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 ("failed to load worker information");} finally {HibernateSessionFactory. closeSession ();} Return worker;}/* worker = (Worker) session. get (Worker. class, id); HQL Hibernate printed on the console: 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 // connect to the person worker0_1 _ on worker0 _ In the person table _. wid = worker0_1 _. id where worker0 _. wid =? // The parameter id is connected to the Wid field of the worker table */}
Test. jsp

 

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="com.hust.Dao.*" %><%@ page import="com.hust.PO.*" %>
Inheritance relationship of hibernate (2) one table for each subclass <% // New Student object 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 ("Huazhong University of Science and Technology"); // Save the stu object TableDao = new TableDao (); dao. saveStu (stu); // get stu object from the database Student stu1 = dao. loadStu (stu. getId (); // when loading a subclass object, the inner join out statement is used. println ("
Student name in the database: "+ stu1.getName (); // total attribute out. println ("
Student ID in the database: "+ stu1.getSno (); // feature attribute out. println ("
Student school in the database: "+ stu1.getSchool (); // feature attribute out. println ("
"); // Create a Worker object worker Worker = new worker (); Worker. setId (new Integer (2); worker. setName ("Li Si"); worker. setAge (new Integer (34); worker. setSex ("nan"); worker. setWno ("W20152223"); worker. setSalary (new Double (5435.32); // Save the dao object of the worker. saveWorker (worker); // obtain the Worker worker2 = dao from the database. loadWorker (worker. getId (); // when loading a subclass object, the inner join and out statements are used. println ("
Name of the worker in the database: "+ worker2.getName (); // total attribute out. println ("
Worker ID in the database: "+ worker2.getWno (); // feature attribute out. println ("
Worker Salary in the database: "+ worker2.getSalary (); // feature attribute %> result:

 


HQL printed on the console:

 

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        person        (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_.Sid=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        person        (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=?






 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

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.