Entity Inheritance Using Mapped Superclass

When I engaged myself into code re-factoring of our VELACORE project, I found that almost all Entities have four common properties createdBy, creationDate, modifiedBy and modificationDate.  I wanted to reduce this code duplication and found that JPA is providing Mapped Superclass for this type of requirement where state and mapping information are common to multiple Entity classes. It contains persistent state and mapping information, but is not Entitity. That is, it is not decorated with the @Entity annotation and is not mapped as an entity by JPA.

Mapped Superclasses do not have any corresponding tables in the underlying Datastore (that was also my requirement). Entities that inherit from the mapped Superclass define the table mappings. Mapped superclasses are specified by decorating the class with the javax.persistence.MappedSuperclass annotation.

Here is an example of using Mapped Superclass for entity inheritance

@MappedSuperclass
public class BaseEntity
{
    @Column(name = "created_by")
    private String createdBy;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "creation_date")
    private Calendar creationDate;
    
   //getter setter of the properties ....
}

@Entity
public class Customer extends BaseEntity 
{
    @Column(name = "first_name")
    private String firstName;

    ...
}

@Entity
public class Invoice extends BaseEntity {
    @Column(name = "invoice_no")
    private String invoiceNo;

    ...
}


In the above code snippet BaseEntity is the MappedSuperclass which contains the mapping information of the common properties. Customer and Invoice are two Entities extending the BaseEntity. In this scenario only CUSTOMER and INVOICE tables will be created in the database not the BASEENTITY. "created_by" and "creation_date" columns will be present in both CUSTOMER and INVOICE tables.


Limitations of using Mapped Superclass :
-Mapped Superclasses are not queryable, and can’t be used in EntityManager or Query operations.
-Mapped Superclasses can’t be targets of entity relationships.

6 comments:

Maksud January 2, 2011 at 9:18 PM  

Good Idea.

Anonymous March 20, 2013 at 5:39 AM  

I have one a confusion.
Suppose I have 7 kind of different field so that I have make division in four different classes.

But, what I have to do when some class wants to extends all te features(here property) of all the classes. Here java does not providing multiple inheritance.

Can You help me please???

pradip.bhatt@aspiresoftware.in

Business April 24, 2013 at 5:04 AM  

Thanks for the info. Your post made clear what Mapped Superclass really is.

Anonymous May 6, 2013 at 10:20 AM  

Can you persist and query the concrete classes? If you can do you have to specify the concrete classes within your entity manager and not the base class? What if I want to use my base class within the entity manager, but persist my concrete classes?

Pohsun May 23, 2013 at 8:16 PM  

The information is useful to me, thanks.

Unknown September 20, 2013 at 2:49 AM  

This information is useful..

Could you kindly provide a working example of mappedsuperclass having annotation based named queries.

Thanks,
Deepak

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers