JPA Hibernate Inheritance Mappings

Source: Internet
Author: User

In the field of object-oriented programming, there are inheritance relationships between classes and classes, such as Java the world needs only extends keyword to determine the parent-child relationship of these two classes, but in the relational database, there are some knowledge correlation relationships. In order to map inheritance to a relational database,Hibernate provides 3 ways to

    • The entire inheritance system is annotated with a single table (single_table): @Inheritance (Strategy =inheritancetype.single_table )
    • Each class is a table, and the subclass holds the unique genus. Sex notes: @Inheritance (strategy= inheritancetype.joined)
    • each concrete class is a table, a subclass of properties unique to it. Corresponding annotations: note:@Inheritance (Strategy =inheritancetype.table_per_class)

in the A nnotation is used in the @inheritance annotations, and you want to use Strategy how policy is specified

Specific examples

now suppose that there is people, student, teacher three classes,

1· Single table

The way single_table is to set the parent class and all its subclasses together in a single table, and create a new field to determine the type of the object.

through @Enitty Introducing Entities

@ Table (name= " tables name ")

@colum column name  

@Entity @table (name = "People") @Inheritance (strategy = inheritancetype.single_table) @DiscriminatorColumn (name = "Emp_ Type ") public class people  implements Serializable {private static final long Serialversionuid =- 7674269980281525370L; @Id @generatedvalue (strategy = generationtype.identity) protected Integer Id; @Columnprotected String name;//Getter/setter Method}

Student Entities

@Entity @discriminatorvalue ("ES")//distinguished field public class Student extends people  {private static final long Serialversionuid = 9115429216382631425L; @Columnprivate int classname;//Getter/setter Method}

Teacher Entity

@Entity @discriminatorvalue ("PT") public class Teacher  extends people{private static final long Serialversionuid =- 6122347374515830424L; @Column (name = "Hourly_wage") Private Double salary//Getter/setter method}

in the example above, only one table is generated, containing the fields Emp_type, Id, name, salary, ClassName. When you save student, the value of Emp_type is "ST", and when you save Teacehere, the value of Emp_type is "PT".


2· Each subclass of a table, storing its own unique characteristics.

This policy superclass is mapped to a separate table, and each subclass is mapped to a separate table. The corresponding table in the subclass includes only the fields corresponding to its own property, and by default the primary key is used as the foreign key for the table corresponding to the superclass (and also the value primary key, called the unique foreign key).

will also generate a table for the superclass, with fewer distinguished fields.

The basic wording is as follows:

@Entity @table (name = "People") @Inheritance (strategy = inheritancetype.joined)//different public class of the key strategy people  Implements Serializable {private static final long serialversionuid = -7674269980281525370l; @Id @generatedvalue ( Strategy = generationtype.identity) protected Integer Id, @Columnprotected String name;//Getter/setter Method}

Student Entities

@Entity @table (name = "Student") public class Student extends people  {private static final long Serialversionuid = 91154 29216382631425L; @Columnprivate int classname;//Getter/setter Method}

Teacher Entity

@Entity @table (name = "Teacher") public class Teacher  extends people{private static final long Serialversionuid = 6122 347374515830424L; @Column (name = "Hourly_wage") Private Double salary//Getter/setter method}

This map generates the third chapter table, Span lang= "en-US" style= "Font-family:simsun" >people student teacher people stuent teacher

people the primary key in the ID as a Student , Teacher the foreign key, and also the primary key exists. the subclass saves a single piece of data, inserts a record in the people , and then inserts the data into the subclass table.


3• One table per specific class

Each specific class of this mapping strategy is mapped to a separate table, and all properties of the class, including inherited properties, are mapped to the columns of the table

Specific examples We look at the parent class

@Entity @table (name = "People") @Inheritance (strategy = inheritancetype.single_table) Public  abstract class people Implements Serializable {//abstract class private static final long serialversionuid = -7674269980281525370l; @Id @generatedvalue ( Strategy = generationtype.identity) protected Integer Id, @Columnprotected String name;//Getter/setter Method}

of the parent class Abstract The attribute is not mapped to a specific table.

Summary:

above is JPA of the Hibernate the implementation of the inheritance map. Compared with the hibernater configuration, it is much simpler, but less flexible, while the versatility of the configuration is greatly improved.


JPA Hibernate Inheritance Mappings

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.