Skip to content

Latest commit

 

History

History

HibernateSpringBootMappedSuperclassRepository

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

How To Handle Entities Inheritance With Spring Data Repositories

Description: The @MappedSuperclass is an entity-level annotation useful for shaping an inheritance model similar with table-per-class strategy but with a base class that is not an entity - is not materialized in a database table. Consider the following entity hierarchy (Book is annotated with @MappedSuperclass):

For these two entities, Paperback and Ebook, we have the corresponding repositories, PaperbackRepository and EbookRepository. But, if we write a query-method as findByTitle() we should duplicate it in both of these repositories in order to call PaperbackRepository#findByTitle() and EBookRepository#findByTitle(). But, we know that Paperback and Ebook are actually subclasses of Book, therefore they inherit the Book class. It will be useful to do the same thing for our repositories. It will be better to write the findByTitle() query-method only once and use it in our repositories instead of duplicating it in each repository. This application shows you how to do it.

Key points:

  • define the findByTitle() in a @NoRepositoryBean class (let's name this class, BookBaseRepository)
  • next, PaperbackRepository and EbookRepository extends BookBaseRepository
  • for queries express via @Query use #{#entityName} and Spring will replace it with the proper entity name

If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.