Extended entities

JPA supports the ability to map entities that share a super class. The object's inheritance is not mapped, but the mapping of the superclass is shared and therefore reused. This is in contrast to the entity inheritance modeling in the previous section.

Mapped super-classes

A mapped superclass is a non-entity class that contains metadata information: a class that has persistence annotations, which is quite similar to the table-per-class inheritance, but has no support or ability to be queried, stored, and modified into the database. A mapped superclass is not an entity and therefore has no equivalent database table (or view) applied.

The entity subclasses of the mapped superclass contain the necessary persistence capable information in order to map the object instance into a database table.

The annotation @javax.persistence.MappedSuperclass declares an object class an extended persistence capable object, but it is not an entity. A mapped super class can declare persistent fields and accessor properties. It is non-entity and therefore cannot be part of queries, JPQL, or other entity relationships.

Here is an example of the Marketing class as a mapped superclass:

@MappedSuperclass
public abstract class Marketing implements java.io.Serializable {
    @Id @GeneratedValue    
    private int id;

    private BigBecimal budget;
    /* As before in the single table strategy... */

    public Marketing() { }
    /* ... */
}

This version of Marketing is not an entity and therefore cannot be queried. Hence there are named queries applied to this class; they have been removed.

Here is the AgencyMarketing entity using the mapped superclass:

@Entity
@Table(name="AGENCY_MARKETING")
@NamedQueries({
    @NamedQuery(name="AgencyMarketing.findAll",
                query="SELECT m FROM AgencyMarketing m"),
    @NamedQuery(name="AgencyMarketing.findByCampaign",
                query="SELECT m FROM AgencyMarketing m WHERE m.campaign = :campaign"),
}) 
public class AgencyMarketing extends Marketing {
    private String agency;
    private BigDecimal agencyBudget;
  
    public AgencyMarketing() { }
    /* ... */
}

The entity AgencyMarketing is mapped to a single database table AGENCY_MARKETING. It has all of the columns defined in the Marketing superclass and its own class. This entity can have named queries, which only retrieve the instances of the AgencyMarketing, since this class is not taking part in entity inheritance strategy.

The @MappedSuperclass is very useful for applications that share a set of common persistence fields across entities. Developers can provide persistent properties across a whole domain, for example an auditing mapped superclass is easily written.

JPA provides another annotation @javax.persistence.AttributeOverride. This is useful to override the mapping in a super class. It requires the name of the field or the property accessor and then the configuration for the new database column.

Here is an example of its use in the DirectMarketing entity:

@Entity
@Table(name="DIRECT_MARKETING")
@AttributeOverride(name="budget",
    column=@Column(name="FIELD_BUDGET"))
@NamedQueries({
    @NamedQuery(name="DirectMarketing.findAll",
                query="SELECT m FROM DirectMarketing m"),
    @NamedQuery(name="DirectMarketing.findByCampaign",
                query="SELECT m FROM DirectMarketing m WHERE m.campaign = :campaign"),
}) 
public class DirectMarketing extends Marketing {
    private String customerOrientation;
    /* ... */

    public DirectMarketing() { }
    /* ... */
}

The application of @AttributeOverride to DirectMarketing entity overrides the budget field of the Marketing mapped super class into the database column name called FIELD_BUDGET for the database table DIRECT_MARKETING.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.200.14