A summary of 3 inheritance mapping relationships in Hibernate--TPH,TPS,TPC

Source: Internet
Author: User

Java class has an inheritance relationship, the corresponding in hibernate, there is an inheritance relationship, the subclass reaction to the database, there are many forms of implementation, the subclass and the parent class can be mapped to the same table, the subclass can also be mapped to a single table, but with a different label implementation, the subclass table and the parent class table relationship is also different.

Here's a summary of the previous project.

In order to reflect the inheritance relationship in the program domain to the data, Hibernate provides us with 3 scenarios:

First scenario: Each class inherits a tree table (table per Hierarchy) TPH

Second scenario: one table per subclass (table per subclass) TPS

The third scenario: a table per specific class (table per concrete Class) TPC

There are three ways to illustrate the inheritance relationship:


The corresponding three po:

Person.java

Package Com.hust.po;public class Person {     private Integer ID;        private String name;   Name     private Integer age;    Age     private String sex;     Gender    //omit getter and setter functions  }
Student.java

Package Com.hust.po;public class Student extends person {     //private Integer sid;   TPS for each subclass of a table, to add this property, the student ID, and the parent class within the person connected to the       private String Sno;    Study number       private String school;  School              //omit getter and setter functions}
Worker.java

Package Com.hust.po;public class Worker extends person {//private Integer wid;      TPS for each subclass of a table, add this property, worker ID, to the parent class within the person connected to the private String wno;      Work number         private Double salary;   Wages     
I. each tree class inherits a tree table (table per Hierarchy) TPH

http://blog.csdn.net/tuke_tuke/article/details/49981013

A table of the so-called inheritance tree, that is, the subclass attribute and the parent class attribute are placed in a table, but to specify the discriminator column , use subclass must have a column of the judging type, the discriminator specifies which type the record belongs to

Person Table


There is only one parent class person table, only one mapping file Person.hbm.xml


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 ">The so-called mapping of the entire inheritance tree to the same table, that is, the subclass information, is all mapped to the table of the parent class.The <discriminator> tag is used to create an identity column in the table whose column property specifies the name of the columns for the identity column, and type specifies the types of the identity column。 AttentionBe sure to specify the discriminator, which is in the parent class's mapping table, adding a column of type to identify which subclass the employee belongs to, and to specify the discriminator value in the label of each class. 
Results:



Two, one table per subclass (table per subclass) TPS

http://blog.csdn.net/tuke_tuke/article/details/50082457

The so-called "each sub-Class A table": A table of the parent class, a table for each subclass, the table of the parent class save the public have information, the subclass of the table only save their own unique information

Three-table relationships:


Person table


Student table


Worker table


Only one parent class mapping file Person.hbm.xml, three sheets


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 <joined-subclass> tag needs to contain a key tag that specifies which field is associated between the subclass and the parent class. 
Based on the People.hbm.xml table structure, you can see that the table that corresponds to the parent class holds the public information, the corresponding table of the subclass holds the unique information, and the corresponding table of the child class and parent class is associated with a one-to-one primary key association
You can only create a table for a subclass by using Joined-subclass, and the primary key and foreign key of the table for the child class are the primary keys of their parent class. Note that Joined-subclass cannot be mixed with subclass

Results:

Person table Results Student table results Worker table results



Three, each specific Class A table (table Per concrete Class) TPC

http://blog.csdn.net/tuke_tuke/article/details/50085449

The so-called "a table per concrete class" means that each subclass of an 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

There are only two table relationships:


Student table


Worker table


Note: As long as a parent class mapping table Person.hbm.xml, two tables



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 "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 (It seems that the table structure is generated from the mapping file, not very understanding), but no data is inserted.

Set the parent class as an abstract class, and in the mapping file, the parent class is set to Abstract= "true", then the database will not generate the corresponding table of the parent class, the parent class will only play an abstract role.


The Union-subclass is to add the attributes from the parent class to the corresponding table of the child class.

Results:

Student table


Worker table



Advantages and disadvantages of three different ways:

1, first the field of the zone molecule class introduced in the table, that is, the fields that describe the other fields are included. Second, if a property of a subclass cannot be empty, then the field cannot be set at the database level not NULL (non-empty), easy to maintain, only need to modify a table, the flexibility is poor, the table of redundant fields will increase with the number of subclasses more and more . in any case, you only need to work with one table, and a persistent session for a single object requires only one table


2, this design method fully conforms to the design principle of the relational model, and there is no redundancy, maintenance is more convenient, the modification of each class only need to modify its corresponding table, flexibility is very good, is completely reference to the object inheritance of the way to configure, for the parent class query needs to use the left outer link, For subclass queries, you need to use an inner link to handle at least two tables for the persistence of subclasses


3, this design approach conforms to the design principle of the relational model, but there is a problem with duplicate fields in the table. If you need to modify the base class, you need to modify the base class and all the tables corresponding to the subclass of the class, the flexibility of the mapping, the subclass can include the base class attributes of each property to be individually configured, for the subclass of the query only need to access a separate table, the parent query how to retrieve all the tables, Only one table is required for a single object persistent session operation




















A summary of 3 inheritance mapping relationships in Hibernate--TPH,TPS,TPC

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.