Mappedsuperclass entity relationship

Inheritance Mapping - Object Relational Mapper (ORM) - Doctrine

mappedsuperclass entity relationship

JPA also defines a mapped superclass concept defined though the Cannot query, persist, or have relationships; Subclass does not want to inherit @Entity @Inheritance @DiscriminatorColumn(name="PROJ_TYPE"). You must use entity subclasses of the mapped superclass in EntityManager or Query operations. Mapped superclasses can't be targets of entity relationships. If the BaseEntity were not annotated with @MappedSuperclass, the @Id or because this is the best way to map this type of relationship.

How to inherit properties from a base class entity using @MappedSuperclass with JPA and Hibernate

The definition of the superclass with the table per class strategy looks similar to any other entity definition. You annotate the class with Entity and add your mapping annotations to the attributes. The only difference is the additional Inheritance annotation which you have to add to the class to define the inheritance strategy.

The definitions of the Book and BlogPost entities are identical to the previously discussed mapped superclass strategy. You just have to extend the Publication class, add the Entity annotation and add the class specific attributes with their mapping annotations. The table per class strategy maps each entity to its own table which contains a column for each entity attribute. That makes the query for a specific entity class easy and efficient.

mappedsuperclass entity relationship

The superclass is now also an entity and you can, therefore, use it to define a relationship between the Author and the Publication entity. This allows you to call the getPublications method to get all Publications written by that Author.

Hibernate will map each Publication to its specific subclass. The Java code looks easy and comfortable to use.

But if you have a look at the generated SQL statement, you recognize that the table model makes the required query quite complicated. Depending on the amounts of records in both tables, this query might become a performance issue. And it gets even worse if you add more subclasses to the inheritance structure.

You should, therefore, try to avoid these kinds of queries or choose a different inheritance strategy. Single Table The single table strategy maps all entities of the inheritance structure to the same database table. This approach makes polymorphic queries very efficient and provides the best performance. But it also has some drawbacks.

The attributes of all entities are mapped to the same database table. That can create data integrity issues, and your database administrator might not be too happy about it. This is information is stored in a discriminator column which is not an entity attribute.

mappedsuperclass entity relationship

The definition of the subclasses is again similar to the previous examples. But this time, you should also provide a DiscriminatorValue annotation. It specifies the discriminator value for this specific entity class so that your persistence provider can map each database record to a concrete entity class. As I explained at the beginning of this section, the single table strategy allows easy and efficient data access.

The only thing that Hibernate needs to add to the SQL query to fetch a particular entity class is a comparison of the discriminator value.

The previously discussed inheritance strategies had their issues with polymorphic queries. They were either not supported or required complex union and join operations. All entities of the inheritance hierarchy are mapped to the same table and can be selected with a simple query. The following code and log snippets show an example for such a query. It then uses the discriminator value to select the right entity class and to map the database record. Joined The joined table approach maps each class of the inheritance hierarchy to its own database table.

This sounds similar to the table per class strategy. But this time, also the abstract superclass Publication gets mapped to a database table. This table contains columns for all shared entity attributes.

They hold only the columns specific for the mapped entity class and a primary key with the same value as the record in the table of the superclass. Each query of a subclass requires a join of the 2 tables to select the columns of all entity attributes. That increases the complexity of each query, but it also allows you to use not null constraints on subclass attributes and to ensure data integrity. The definition of the superclass Publication is similar to the previous examples.

They just extend the superclass, provide an Entity annotation and define the mapping of their specific attributes. You may decorate only concrete entity classes with DiscriminatorValue. If DiscriminatorValue is not specified on an entity in a class hierarchy that uses a discriminator column, the Persistence provider will provide a default, implementation-specific value.

Entity Inheritance - The Java EE 6 Tutorial

This strategy provides good support for polymorphic relationships between entities and queries that cover the entire entity class hierarchy. However, this strategy requires the columns that contain the state of subclasses to be nullable.

This strategy provides poor support for polymorphic relationships and usually requires either SQL UNION queries or separate SQL queries for each subclass for queries that cover the entire entity class hierarchy. Support for this strategy is optional and may not be supported by all Java Persistence API providers. JOINED, the root of the class hierarchy is represented by a single table, and each subclass has a separate table that contains only those fields specific to that subclass.

That is, the subclass table does not contain columns for inherited fields or properties. The subclass table also has a column or columns that represent its primary key, which is a foreign key to the primary key of the superclass table.

This strategy provides good support for polymorphic relationships but requires one or more join operations to be performed when instantiating entity subclasses. This may result in poor performance for extensive class hierarchies.

mappedsuperclass entity relationship

Similarly, queries that cover the entire class hierarchy require join operations between the subclass tables, resulting in decreased performance.