Inheritance mapping strategy for JPA Object Relational model through hibernate

Source: Internet
Author: User
Keywords Hibernate Object Relational model inheritance mapping strategy
Tags abstract added class default different information joined listing

Single-table policy: This is the default policy in the inheritance map, which is mapped using this mapping strategy, not specifically specified. The mapping principle for this policy is that the parent class, including the newly added attributes in the subclass, is mapped to a database table, and an automatically generated field in the database table is used to store information that distinguishes the different subclasses.

Joined-subclass policy: In this mapping strategy, each entity class in the inheritance relationship, whether a specific class (concrete entity) or abstract class (abstract entity), has a separate table in the database that corresponds to him. A child entity corresponds to a table that does not contain attributes inherited from the root entity, and is associated in a way that is shared by a primary key.

Table-per-concrete-class policy: This strategy is to map each entity in an inheritance relationship to a separate table in the database, and unlike the "joined" policy, a child entity corresponds to a table that contains attributes inherited from the root entity. This strategy is still freely chosen in the JPA2.0, that is to say, implementing the JPA2.0 specification's persistence engine, such as Toplink,hibernate, is still free to choose whether or not to implement this strategy.

Thanks to the use of the annotation (annotation) approach, the mapping of the inheritance relationship in JPA 2.0 is straightforward. When there is an inheritance relationship between entities (entity), there must be a root entity (root entity), in which the annotation @Inheritance annotation on the root entity (root entity) is required and the mapping strategy you want to use is indicated. The default is to use a single table policy (Single-table strategy) If you do not need to @Inheritance annotations, or if you use @Inheritance annotations but do not indicate the mapping strategy you want to adopt. The following inheritance relationship with Figure 1 illustrates the use of these three mapping strategies respectively.

Diagram 1.Item, Magazine, Phone inheritance relationship UML class diagram

The item class defines the Title, price, and Description attributes, where the Magazine class and the Phone class inherit the item class and add their own unique properties, Magazine add ISBN and Publisher properties to the The Factory and Durationtime properties are added to the Phone.

Single Table mapping policy

A single table (single-table) map is the default mapping strategy in an inheritance map, which is used by default when not specified in the root entity (root entity). In this example, the root entity (root entity) refers to the entity Item class.

Listing 1. Item entity defined as a single table map

@Entity public class Item implements Serializable {private static final long serialversionuid = 1L; @Id @GeneratedValue ( Strategy = Generationtype.auto) private Long ID; Private String title; Private Float Price; Private String decription; Getters and Setters}

The Item entity class is not annotated with @Inheritance this annotation (annotation), stating that a single table mapping strategy is used in this inheritance relationship. The item entity class is also the root entity of the Phone entity class and the Magazine entity class (Root entity), which inherits the item's properties and has its own unique properties.

Listing 2.Phone entity inherits from Item entity

@Entity public class Phone extends Item {private String http://www.aliyun.com/zixun/aggregation/16557.html ">factory; Private Float Durationtime; Getters and Setters}


Listing 3.Magazine entity inherits from Item entity

@Entity public class Magazine extends Item {private string ISBN; private string publisher;//getters and Setters}

Related Article

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.