SSH Development Practice part3:hibernate Inheritance Mapping

Source: Internet
Author: User
Tags class manager unique id

0

Hello everyone. Last time I talked about the mapping configuration of two-way 1-n in Hibernate, you can refer to: http://www.cnblogs.com/souvenir/p/3784510.html

In actual projects, the relationship between objects is more complex, except for the last one, this time we are going to talk about object inheritance. How can hibernate be configured to complete inheritance of objects?

1

For example, there is a parent class person, and then two corresponding subclasses, one is teacher, and the other is student. Teachers and teachers have their own unique properties in addition to all the attributes of the person class.

There are three ways hibernate provides the mapping method:

A a sub-class corresponds to a table

In other words, each subclass creates a new table, the attributes of which are added to the attributes of the parent class, and then the individual attributes are added separately.

This undoubtedly adds a lot of redundancy, so it is not recommended.

B use a large table to represent the collection of attributes for all subclasses

For example, the parent class has 3 public properties, teacher this subclass has 2 separate attributes, student has 3 individual attributes, then we need to create a table to include all of the 8 attributes.

Then in a field to differentiate between these object entities specifically belong to that subclass of

The C parent class table stores public properties, each of which stores its own individual properties and then associates them with a unique ID

2 "Use a large table to represent the collection of attributes for all subclasses"

Or use the previous employee object to do the demo, now we need to inherit a subclass manager under the employee

This subclass is currently trying to demonstrate that we only add a separate attribute: department

Take a look at the Hibernate configuration file:

  

<hibernate-mapping Package= "Com.souvenir.bean">    <classname= "Employee"Table= "Employee"Discriminator-value= "E">        <!--Mapping Identity Properties -        <IDname= "id"column= "emp_id"type= "int">            <!--To specify a primary key generator policy -            <Generatorclass= "Identity"/>        </ID>                <discriminatorcolumn= "Emptype"type= "string"></discriminator>                <!--Map Normal Properties -        < Propertyname= "Name"type= "string"/>        < Propertyname= "Password"type= "string"/>        < Propertyname= "Age"type= "int"/>                                  <!--Employee sub-class manager -        <Subclassname= "Manager"extends= "Employee"Discriminator-value= "M">            < Propertyname= "Dept"type= "string"length= " the"column= "Dept_name"/>        </Subclass>    </class></hibernate-mapping>

What you need to explain is:

    • This is done by using the subclass keyword to complete the configuration of subclasses
    • The parent class needs to specify a distinguished field for the discriminator keyword class
    • All subclasses add the Discriminator-value attribute to differentiate their object (or add it if required by the parent class)

Let us test the effect of the map, the specific test code I will not write, probably is to save a few manager and employee objects

  

As can be seen from the database results, the first 10 are employee objects, Emptype are E

The last one is the manager object, Emptype is M

This way you can see that there are a lot of redundant fields, there is no dept_name attribute for employee, so this property is null

3 "The parent class table stores public properties, each of which stores its own individual properties, and then associates with a unique ID"

In this case we need to use the keyword Joined-subclass to add all subclasses, in addition to the need to configure separate attributes

You need to specify a key to associate with the parent class. (via the Column property)

Here's a look at the configuration modified above:

<hibernate-mapping Package= "Com.souvenir.bean">    <classname= "Employee"Table= "Employee" >        <!--Mapping Identity Properties -        <IDname= "id"column= "emp_id"type= "int">            <!--To specify a primary key generator policy -            <Generatorclass= "Identity"/>        </ID>                <!--Map Normal Properties -        < Propertyname= "Name"type= "string"/>        < Propertyname= "Password"type= "string"/>        < Propertyname= "Age"type= "int"/>        <!--relationship to the manager -        <Many-to-onename= "Manager"class= "Manager"column= "mgr_id"Lazy= "false"/>                    <!--correlation between mappings and payment -        <Setname= "payments"Inverse= "true"Lazy= "false">            <Keycolumn= "emp_id" />            <One-to-manyclass= "Payment"/>        </Set>            <!--Employee sub-class manager -        <Joined-subclassname= "Manager"extends= "Employee"  >             <Keycolumn= "emp_id"></Key>              < Propertyname= "Dept"type= "string"length= " the"column= "Dept_name"/>       </Joined-subclass>    </class></hibernate-mapping>

Note that the discriminator configuration is no longer needed here.

Here's a look at the test results: (yes, the test code doesn't have to change)

First, there are two tables in the database, one is employee, the other is manager

  

   

As can be seen here, all public properties exist in the parent table, and the child table simply stores the attributes it needs, both of which are associated by emp_id. (mgr_id is my test, people do not care first)

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.