An analysis of the inheritance relationship design in Java Hibernate framework _java

Source: Internet
Author: User

This time we'll take a look at the hierarchical design of hibernate, which is the design of the inheritance relationship between entities.
Perhaps this is more abstract, we look directly at the example.
1 Let's take a look at the common practice
Directly on the code: three real classes are as follows:

public class TItem implements serializable{ 
    //Omit Get/set method 
  private int id; 
  private String manufacture; 
  private String name; 
public class Tbook extends titem{ 
    //Omit Get/set method 
  private int pagecount; 
   
} 
public class Tdvd extends titem{ 
    //Omit Get/set method 
  private String Regioncode; 


Here we need three mapping files, as follows:

<class name= "TItem" table= "ITEM" > <id name= "id" column= "id" type= "java.lang.Integer" > <generat or class= "native"/> </id> <property name= "name" column= "name" type= "java.lang.String"/> ;p roperty name= "manufacture" column= "manufacture" type= "java.lang.String"/> </class> <class name= "Tbook" 
    table= "book" > <id name= "id" column= "id" type= "java.lang.Integer" > <generator class= "Native"/> </id> <property name= "name" column= "name" type= "java.lang.String"/> "<property name=" manufact Ure "column=" manufacture "type=" java.lang.String "/> <property name=" PageCount "column=" PageCount "type=" Java.l Ang. Integer "/> </class> <class name=" Tdvd "table=" DVD "> <id name=" id "column=" id "type=" java.lang.i Nteger "> <generator class=" native "/> </id> <property name=" name "column=" name "Type=" J Ava.lang.String "/>
    <property name= "manufacture" column= "manufacture" type= "java.lang.String"/> <property "name=" 
 Column= "Regioncode" type= "java.lang.String"/> </class>

A very common mapping file, no different from the previous.
Here we write a test method directly:

public void Testselect () { 
     
    query query = Session.createquery (' from TItem '); 
    List List = Query.list (); 
    Iterator iter = List.iterator (); 
     
    while (Iter.hasnext ()) { 
      System.out.println ("Name:" + ((TItem) Iter.next ()). GetName ()); 
    } 
     
   

Note that here we use the TItem class, not the concrete word class, where it automatically looks for subclasses that inherit from the TItem class and finds all the results. This involves a polymorphic pattern, the class tag has attribute polymorphism, and its default value is implicit, which means that the result can be queried without the need to specify a name. If explicit indicates that a specific class name needs to be specified, the results of this class can be detected.
2 The last example we used three mapping files, when we need to modify, we need to modify three mapping files, which is not feasible for large projects. And each table has a corresponding field for the main class, which is redundant. So we have the following method.
The entity class is the same as in 1. We change the mapping file from three to one, leaving only the TItem mapping file. But we need to make the corresponding changes, now the contents are as follows:

 <class name= "TItem" table= "ITEM" polymorphism= "explicit" > <id name= "id" col umn= "id" type= "java.lang.Integer" > <generator class= "native"/> </id> <property name= "n Ame "column=" name "type=" java.lang.String "/> <property name=" manufacture "column=" manufacture "type=" Java.lang . String "/> <joined-subclass name= tbook" table= "Tbook" > <key column= "id"/> Name= "PageCount" column= "PageCount" type= "Java.lang.Integer"/> </joined-subclass> <joined-subclass Name= "Tdvd" table= "Tdvd" > <key column= "id"/> <property name= "Regioncode" column= "Regioncode" Typ E= "java.lang.String"/> </joined-subclass> </class> 


Here, we have only one mapping file, but there is a joined-subclass tag, which indicates that the class inherits from the current class,<key> the primary key of the table, where the table refers to the table Tbook and Tdvd of the two subclasses. Only fields in the table are specified in the property.
So when we run, the resulting table is the following figure:

The table of two subclasses corresponds only to the fields that we specify through the property. This avoids having multiple fields in the table so that the Word table maintains only its individual fields, and when the item class is changed, there is no need to modify it too much.

3 to understand another way to implement a hierarchical design, which is achieved by entering a flag inside a table. In the hibernate mapping file we implement the Descriminator tag.
Nonsense not much to say, we directly look at the example:
We modified yesterday's TItem mapping file to:

<class name= "TItem" table= "ITEM" polymorphism= "explicit" > 
    <id name= "id" column= "id" type= " Java.lang.Integer "> 
      <generator class=" native "/> 
    </id> 
     <discriminator column=" Category "Type=" java.lang.String "/> 
      
     <property name=" name "column=" name "type=" java.lang.String "/> 
     <property name= "manufacture" column= "manufacture" type= "java.lang.String"/>     
  </class> 

In the middle, we added a discriminator tag that indicates which of the following two subclass is to be distinguished by which field.

<subclass name= "Tbook" discriminator-value= "1" > <property name= "PageCount" column= 
    "PageCount"/> 
   </subclass> 
   <subclass name= "Tdvd" discriminator-value= "2" > 
    <property name= "Regioncode" "column=" Regioncode "/> 
   </subclass> 

We see these two paragraphs, which indicate that when the value of the field specified by Discriminator is 1 o'clock, it indicates that it is a tbook class and that PageCount has a value, and that the field value specified by Discriminator is 2 o'clock, indicating that it is a Tdvd class, And Regioncode has a value.
So we just need to use a table, it shows the relationship between their several classes, note that this approach to have too many subclasses, and not good, it will make the main table field too much, will cause a certain design inconvenience.

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.