Entity hierarchy design in JPA

Source: Internet
Author: User

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

1.Single Table strategyA 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 subclass corresponds to a table, and each table has the attribute of the base class.

3.Join strategyIs still a table corresponding to each subclass, but this table does not contain the attributes of the base class, only the extended attributes of this subclass, share 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;

.......

.......

This part of content is completely consistent with hibernate. Java ee5 has learned a lot from spring and hibernate.

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.