Instructions for use of @MappedSuperclass annotations

Source: Internet
Author: User

Reprinted from: http://blog.sina.com.cn/s/blog_7085382f0100uk4p.html

Based on the idea of code reuse and model separation, the JPA @mappedsuperclass annotations are used in project development to encapsulate multiple attributes of an entity class into separate non-entity classes.

1.@MappedSuperclass annotations can only be standard on the class: @Target ({Java.lang.annotation.ElementType.TYPE})

2. The class labeled @mappedsuperclass will not be a full entity class and will not be mapped to a database table, but his properties will be mapped to the database fields of their subclasses.

3. classes labeled @mappedsuperclass can no longer annotate @entity or @table annotations, nor do they need to implement a serialization interface.

But if a class labeled @mappedsuperclass inherits another entity class or another class with the same label @mappedsuperclass, He will be able to use @attributeoverride or @attributeoverrides annotations to redefine the properties of its parent class (whether or not an entity class) to a field in a database table.

For example, you can redefine properties such as field names or lengths, and use sub-properties @column in @attributeoverride to define them.

Note: Properties that annotate @lob annotations in their parent class will not be overloaded, and @column settings in @attributeoverride will not work.

The @lob annotations in the JPA specification do not indicate that @column annotations cannot be annotated at the same time, but in practice hibernate JPA does not support this notation.

4. In addition, such a class can also directly label the @entitylisteners entity Listener, whose scope is only in all of its inherited classes, and the entity listener can also be guaranteed to be inherited or overloaded by its subclasses.

5. class labeled @mappedsuperclass its properties are best set to protected or default type to ensure that subclasses under its same package can invoke its properties directly. Facilitates operation of entity listeners or with parametric constructors.

6. because the class labeled @mappedsuperclass will not be a complete entity class, it cannot be labeled @table and cannot use @uniqueconstraint to set the unique property of the field, as well as overloading the property type The support JPA specification (such as an overloaded attribute labeled @lob) has yet to be improved.

7. @discriminatorvalue annotations can be annotated at the same time to set the value of the entity identifier field for the entity subclass. This property is generally used when the entity inherits, but can not be set when the entity is mapped.

8. Compare entity Inheritance to entity mapping:

The three strategies for entity inheritance are: single_table (all inherited entities are saved in the same database table), JOINED (each entity subclass is saved in a separate table), Table_per_class ( All entity classes that have an inheritance relationship are saved in a separate table.

Entity mapping is most similar to joined entity inheritance, and he also saves entity subclasses as a single table, but the biggest difference is that when querying, joined uses a polymorphic query, and the data of all its entity subclasses will be queried at the same time when querying the parent class, so the query time and performance will be affected. But a database query of entity mapping is equivalent to a query that has no entity inheritance, that is, he represents an inherited relationship only at the entity level and does not embody such a relationship in the database, his operations are independent and will not affect the entity subclasses.

Three inheritance relationships for the JPA entity mappings of Hibernate

Reprinted from: http://www.cnblogs.com/shangxiaofei/p/5704321.html

In JPA, there are three types of mapping strategies for entity Inheritance Relationships: single-Table inheritance policy (table per class), joined policy (table per subclass), and Table_per_class policy.

1. Single-Table inheritance policy

Single-table inheritance policy, where parent and subclass entities share a single database table that distinguishes between different categories of entities in a table through a list of distinguished fields. The following are the specific practices:

A. Add the following annotations under the @entity annotations of the parent entity:

@Inheritance (strategy=inheritancetype.single_table)
@DiscriminatorColumn (name= "distinguish field column name")
@DiscriminatorValue (the parent entity identifies the field column values)

B. Add the following annotations under the @entity annotations of the subclass entity:

@DiscriminatorValue (the subclass entity identifies the field column values)

Defines a parent class

  1. @Entity
  2. @Inheritance (strategy = inheritancetype.single_table)
  3. @Table (name = "Window_file")
  4. @DiscriminatorColumn (name = "Discriminator", Discriminatortype = discriminatortype.string, length = 30)
  5. @DiscriminatorValue ("Windowfile")
  6. public class Windowfile {
  7. @Id
  8. @GeneratedValue (strategy = Generationtype.auto)
  9. Private Integer ID;
  10. @Basic
  11. @Column (name = "Name")
  12. private String name;
  13. @Basic
  14. @Column (name = "TYPE")
  15. Private String type;
  16. @Basic
  17. @Column (name = "DATE")
  18. private date date;
  19. Omit Get Set
  20. }

2 sub-classes after definition

    1. @Entity
    2. @DiscriminatorValue ("Folder")
    3. public class Folder extends Windowfile {
    4. @Basic
    5. @Column (name = "File_count")
    6. Private Integer FileCount;
    7. Omit Get Set
    8. }

    1. @Entity
    2. @DiscriminatorValue ("Document")
    3. public class Document extends Windowfile {
    4. @Basic
    5. @Column (name = "SIZE")
    6. private String size;
    7. Omit Get Set
    8. }

The above is different from the column discriminator, distinguish the specific parent-child entity.

The actual table structure is as follows:

Window_file Discriminator,id,name,date,type,size,file_count

When you use the Windowfile entity, the actual table field is discriminator= ' windowfile ', size and file_count are always empty

When using the folder entity, discriminator= ' folder ', size is always empty, file_count is the actual value.

Document is similar to folder.

2.Joined strategy

The parent and subclass entities correspond to different tables in the database, and only the special attributes of their extensions exist in the table of the child entity, and the public properties of the parent class are saved in the parent Class entity mapping table. Specific practices:

@Inheritance (strategy=inheritancetype.joined)

Subclass entities do not require special instructions.

  1. @Entity
  2. @Table (name = "T_animal")
  3. @Inheritance (strategy = inheritancetype.joined)
  4. public class Animal {
  5. @Id
  6. @Column (name = "ID")
  7. @GeneratedValue (strategy = Generationtype.auto)
  8. Private Integer ID;
  9. @Column (name = "Name")
  10. private String name;
  11. @Column (name = "COLOR")
  12. private String color;
  13. Omit Get Set
  14. }

    1. @Entity
    2. @Table (name = "T_bird")
    3. @PrimaryKeyJoinColumn (name = "bird_id")
    4. public class Bird extends Animal {
    5. @Column (name = "Speed")
    6. Private String speed;
    7. Omit Get Set
    8. }

    1. @Entity
    2. @Table (name = "T_dog")
    3. @PrimaryKeyJoinColumn (name = "dog_id")
    4. public class Dog extends Animal {
    5. @Column (name = "LEGS")
    6. Private Integer legs;
    7. Omit Get Set
    8. }

The actual table structure is as follows:

T_animal Id,color,name

T_bird Speed,bird (both a foreign key and a primary key)

T_dog legs,dog_id (both a foreign key and a primary key)

3.table_per_class Policy:

Table_per_class policy, the parent and child class entities each correspond to a table in a database, and all attributes are saved in the subclass table, including attributes inherited from the parent class entity. Specific practices:

Simply add the following annotations under the @entity annotations of the parent entity:

@Inheritance (Strategy=inheritancetype.table_per_class)

    1. @Entity
    2. @Inheritance (strategy = Inheritancetype.table_per_class)
    3. @Table (name = "T_vehicle")
    4. public class Vehicle {//base class
    5. @Id
    6. @GeneratedValue
    7. @Column (name = "ID")
    8. Private Integer ID;
    9. @Column (name = "Speed")
    10. Private Integer speed;//Speed
    11. Omit Get Set
    12. }

    1. @Entity
    2. @Table (name = "T_car")
    3. public class Car extends Vehicle {
    4. @Column (name = "ENGINE")
    5. Private String engine;//Engine
    6. Omit Get Set
    7. }

Once this strategy is used, it means that you cannot use Auto generator and identity generator, that is, the primary key value cannot be automatically generated by the database.

The actual table structure is as follows:

T_vehicle Id,speed

T_car Id,speed,engine

Instructions for use of @MappedSuperclass annotations

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.