I usually post about Persistence on Twitter - you can follow me there:

1. Introduction

Inheritance is one of the key concepts in Java. So, it’s no surprise that most domain models use it. But unfortunately, this concept doesn’t exist in relational databases, and you need to find a way to map the inheritance hierarchy to a relational table model.

JPA and Hibernate support different strategies, that map the inheritance hierarchy to various table models. Let’s take a look at a chapter of my new book Hibernate Tips – More than 70 solutions to common Hibernate problems in which I explain the SingleTable strategy. It maps all classes of the inheritance hierarchy to the same database table.

I explain Hibernate’s other inheritance mapping strategies in my Hibernate Tips book. It’s a cookbook with more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries. You can get it this week on Amazon at a special launch price of just $2.99.


2. Hibernate Tips – How to map an inheritance hierarchy to one table

2.1. Problem

My database contains one table, which I want to map to an inheritance hierarchy of entities. How do I define such a mapping?

2.2. Solution

JPA and Hibernate support different inheritance strategies which allow you to map the entities to different table structures. The SingleTable strategy is one of them and maps an inheritance hierarchy of entities to a single database table.

Let’s have a look at the entity model before I explain the details of the SingleTable strategy. Authors can write different kinds of Publications, like Books and BlogPosts. The Publication class is the super class of the Book and BlogPost classes.

The SingleTable strategy maps the three entities of the inheritance hierarchy to the publication table.

If you want to use this inheritance strategy, you need to annotate the superclass with an @Inheritance annotation and provide the InheritanceType.SINGLE_TABLE as the value of the strategy attribute.

You can also annotate the superclass with a @DiscriminatorColumn annotation to define the name of the discriminator value. Hibernate uses this value to determine the entity to which it has to map a database record. If you don’t define a discriminator column, as I do in the following code snippet, Hibernate, and all other JPA implementations use the column DTYPE.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Publication {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    private int version;

    private String title;

    private LocalDate publishingDate;
	
    @ManyToMany
    @JoinTable(
      name="PublicationAuthor",
      joinColumns={@JoinColumn(name="publicationId", referencedColumnName="id")},
      inverseJoinColumns={@JoinColumn(name="authorId", referencedColumnName="id")})
    private Set<Author> authors = new HashSet<Author>();

    ...
}

The subclasses need to extend the superclass, and you need to annotate them with a @Entity annotation.

The JPA specification also recommends to annotate it with a @DiscriminatorValue annotation to define the discriminator value for this entity class. If you don’t provide this annotation, your JPA implementation generates a discriminator value.

But the JPA specification doesn’t define how to generate the discriminator value, and your application might not be portable to other JPA implementations. Hibernate uses the simple entity name as the discriminator.

@Entity
@DiscriminatorValue("Book")
public class Book extends Publication {

    private int numPages;

    ...
}

The SingleTable strategy doesn’t require Hibernate to generate any complex queries if you want to select a specific entity, perform a polymorphic query or traverse a polymorphic association.

Author a = em.find(Author.class, 1L);
List<Publication> publications = a.getPublications();

All entities are stored in the same table, and Hibernate can select them from there without an additional JOIN clause.

15:41:28,379 DEBUG [org.hibernate.SQL] - 
    select
        author0_.id as id1_0_0_,
        author0_.firstName as firstNam2_0_0_,
        author0_.lastName as lastName3_0_0_,
        author0_.version as version4_0_0_ 
    from
        Author author0_ 
    where
        author0_.id=?
15:41:28,384 DEBUG [org.hibernate.SQL] - 
    select
        publicatio0_.authorId as authorId2_2_0_,
        publicatio0_.publicationId as publicat1_2_0_,
        publicatio1_.id as id2_1_1_,
        publicatio1_.publishingDate as publishi3_1_1_,
        publicatio1_.title as title4_1_1_,
        publicatio1_.version as version5_1_1_,
        publicatio1_.numPages as numPages6_1_1_,
        publicatio1_.url as url7_1_1_,
        publicatio1_.DTYPE as DTYPE1_1_1_ 
    from
        PublicationAuthor publicatio0_ 
    inner join
        Publication publicatio1_ 
            on publicatio0_.publicationId=publicatio1_.id 
    where
        publicatio0_.authorId=?

2.3. Source Code

You can find a download link for a project with executable test cases for this Hibernate tip in the book.

2.4. Learn More

You can also map the entities of the inheritance hierarchy to multiple database tables.
I show you how to do that in the chapter How to map an inheritance hierarchy to multiple tables.

 

3. Summary

As you have seen in this Hibernate Tip, JPA and Hibernate provide an easy option to map an inheritance hierarchy to a single database table. You just have to annotate the superclass with @Inheritance(strategy = InheritanceType.SINGLE_TABLE) and you should also annotate the subclasses with @DiscriminatorValue(“Book”).

You can get more recipes like this in my new book Hibernate Tips: More than 70 solutions to common Hibernate problemsIt gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries. For just a few days, you can get the ebook for $2.99 and the paperback for $12.99 on Amazon.

I usually post about Persistence on Twitter - you can follow me there: