Inheritance ing in hibernate/JPA

Source: Internet
Author: User
Inheritance ing in hibernate

 

Hibernate's inheritance ing includes three different policies:

  1. Each cluster class uses a table;
  2. One table for each subclass;
  3. Each specific table (with restrictions ).

Suppose we have four classes of animal, dog, and CAT. The Code is as follows:
File Name: Animal. Java

Class animal {
Private string identifier;
Private string name;
Private string category;
// Setter and getter
}

File Name: Dog. Java

Class dog extends animal {
Private string
// Setter and getter
}

File Name: Cat. Java

Class cat extends animal {
Private string
// Setter and getter
}

 

  • Each cluster class uses a table.

When you use a table policy for each cluster class, when there is a restriction, the subclass cannot have not null. The ing file is:
File Name: Animal. HBM. xml

<Class name = "animal" table = "tb_animal">
<ID name = "identifier" type = "string" column = "identifier">
<Generator class = "UUID. Hex"/>
</ID>
<Discriminator column = "animal_type" type = "string"/>
<Property name = "name" column = "name" type = "string"/>

<Subclass name = "dog" discriminator-value = "dog">

</Subclass>
<Subclass name = "cat" discriminator-value = "cat">

</Subclass>
</Class>

 

  • One table for each subclass

When using a table policy for each subclass, you can use a ing file or multiple ing files. One ing file for each subclass:
File Name: Animal. HBM. xml

<Class name = "animal" table = "animal">
<ID name = "identifier" column = "identifier" type = "string">
<Generator class = "UUID. Hex"/>
</ID>
<Property>
</Class>
File Name: Dog. HBM. xml
<Joined-subclass name = "dog" table = "dog" extends = "animal">
<Key column = "dog_id"/>

</Joined-subclass>
File Name: Cat. HBM. xml
<Joined-subclass name = "cat" table = "cat" extends = "cat">
<Key column = "cat_id"/>

</Joined-subclass>

The policy of one table for each subclass is actually a one-to-one ing.

  • Each specific table (with restrictions)

When each specific table (with restrictions) is used, the ing file of each subclass will contain all attributes in the parent class. ing file:
File Name: Dog. HBM. xml

<Class name = "dog" table = "dog">
<ID name = "identifier" column = "identifier" type = "string">
<Generator class = "UUID. Hex"/>
</ID>
<Property name = "name" column = "name" type = "string"/>

</Class>
File Name: Cat. HBM. xml
<Class name = "cat" table = "cat">
<ID name = "identifier" column = "identifier" type = "string">
<Generator class = "UUID. Hex"/>
</ID>
<Property name = "name" column = "name" type = "string"/>

</Class>

 

Entity hierarchy design in JPA

This part is basically the same as Hibernate. JPA also supports three types of inheritance:

1. Single Table strategy, single table policy. A table contains all the data of the base class and sub-classes. In many cases, this redundant design is used, which is distinguished by a discriminator.

2. Table per class strategy. Each sub-class corresponds to a table, and each table has the attributes of the base class.

3. Join strategy is still a table corresponding to each subclass, but this table does not contain the attributes of the base class. It is only an extended attribute of this subclass and shares the attributes of the base class.

 

Here is an example to illustrate three cases:

I. Single Table Policy

For example, pet is a base class, cat and dog inherit this class and have their own extended attributes, such:

Package com. denny_blue.ejb3.inheritance;

Import java. Io. serializable;

Import javax. Persistence. discriminatorcolumn;
Import javax. Persistence. discriminatortype;
Import javax. Persistence. entity;
Import javax. Persistence. generatedvalue;
Import javax. Persistence. generationtype;
Import javax. Persistence. ID;
Import javax. Persistence. Inheritance;
Import javax. Persistence. inheritancetype;

@ Entity
@ Inheritance (Strategy = inheritancetype. single_table)
@ Discriminatorcolumn (name = "animal_type", discriminatortype = discriminatortype. String)
Public class pet implements serializable {
Private int ID;

Private string name;

Private double weight;

Public PET (){
}

@ ID
@ Generatedvalue (Strategy = generationtype. Auto)
Public int GETID (){
Return ID;
}

Public void setid (int id ){
This. ID = ID;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public double getweight (){
Return weight;
}

Public void setweight (double weight ){
This. Weight = weight;
}

}

Note that @ inheritance (Strategy = inheritancetype. single_table) determines the single-table policy. @ discriminatorcolumn is used to determine the field and type of the flag value. I 'd like to be familiar with Hibernate and I should be familiar with all these fields. then there are two subclasses:

// Cat. Java

Package com. denny_blue.ejb3.inheritance;

Import javax. Persistence. discriminatorcolumn;
Import javax. Persistence. discriminatortype;
Import javax. Persistence. discriminatorvalue;
Import javax. Persistence. entity;
Import javax. Persistence. Inheritance;
Import javax. Persistence. inheritancetype;

@ Entity
@ Inheritance (Strategy = inheritancetype. single_table)
@ Discriminatorcolumn (discriminatortype = discriminatortype. String)
@ Discriminatorvalue ("cat ")
Public class cat extends pet {
Private string hairball;

Public String gethairball (){
Return hairball;
}

Public void sethairball (string hairball ){
Hairball = hairball;
}

}

// Dog. Java

Package com. denny_blue.ejb3.inheritance;

Import javax. Persistence. discriminatorcolumn;
Import javax. Persistence. discriminatortype;
Import javax. Persistence. discriminatorvalue;
Import javax. Persistence. entity;
Import javax. Persistence. Inheritance;
Import javax. Persistence. inheritancetype;

@ Entity
@ Inheritance (Strategy = inheritancetype. single_table)
@ Discriminatorcolumn (discriminatortype = discriminatortype. String)
@ Discriminatorvalue ("dog ")
Public class dog extends pet {
Private string trick;

Public String gettrick (){
Return trick;
}

Public void settrick (string trick ){
This. Trick = trick;
}

}

The most important concern of the two sub-classes is the @ discriminatorvalue annotation. For example, if the cat value is cat, it means that when the cat type entity is saved to the database, JPA automatically assigns the CAT value to the animal_type field, and the dog value is dog, so that two different child classes can be separated in the same table.

 

Ii. Table per class

If the table per class policy is adopted, each sub-class will create a separate table and have all attributes of the base class independently, the modifications to be made in our example are very small, like this:

// Base class

@ Entity
@ Inheritance (Strategy = inheritancetype. table_per_class)
Public class pet implements serializable {
Private int ID;

Private string name;

Private double weight;

........

// Subclass: No settings are required.

@ Entity
Public class dog extends pet {
Private string trick;

.......

.......

 

Iii. Join Policy

Each subclass can also create tables independently, and the base classes can also create tables independently. However, only the extended attributes of all subclass tables share the base class tables. In our example, modify them:

// Base class

@ Entity
@ Inheritance (Strategy = inheritancetype. Joined)
Public class pet implements serializable {
Private int ID;

Private string name;

Private double weight;

........

// Subclass

@ Entity

@ Inheritance (Strategy = inheritancetype. Joined)
Public class dog extends pet {
Private string trick;

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.