Skip to content

Latest commit

 

History

History

HibernateSpringBootSubentities

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Attributes Lazy Loading Via Subentities

Description: By default, the attributes of an entity are loaded eager (all at once). This application is an alternative to How To Use Hibernate Attribute Lazy Loading from here. This application uses a base class to isolate the attributes that should be loaded eagerly and subentities (entities that extends the base class) for isolating the attributes that should be loaded on demand.

Key points:

  • create the base class (this is not an entity), BaseAuthor, and annotate it with @MappedSuperclass
  • create AuthorShallow subentity of BaseAuthor and don't add any attribute in it (this will inherit the attributes from the superclass)
  • create AuthorDeep subentity of BaseAuthor and add to it the attributes that should be loaded on demand (e.g., avatar)
  • map both subentities to the same table via @Table(name = "author")
  • provide the typical repositories, AuthorShallowRepository and AuthorDeepRepository

Run the following requests (via BookstoreController):

  • fetch all authors shallow (without avatars): localhost:8080/authors/shallow
  • fetch all authors deep (with avatars): localhost:8080/authors/deep

Check as well:


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.