Skip to content

Latest commit

 

History

History

HibernateSpringBootAttributeLazyLoadingBasic

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

How To Use Hibernate Attribute Lazy Loading

If you prefer to read it as a blog-post containing the relevant snippets of code then check this post

Description: By default, the attributes of an entity are loaded eagerly (all at once). But, we can load them lazy as well. This is useful for column types that store large amounts of data: CLOB, BLOB, VARBINARY, etc or details that should be loaded on demand. In this application, we have an entity named Author. Its properties are: id, name, genre, avatar and age. And, we want to load the avatar lazy. So, the avatar should be loaded on demand.

Key points:

  • in pom.xml, activate Hibernate bytecode enhancement (e.g. use Maven bytecode enhancement plugin)
  • in entity, annotate the attributes that should be loaded lazy with @Basic(fetch = FetchType.LAZY)
  • in application.properties, disable Open Session in View

Check as well:
- Default Values For Lazy Loaded Attributes
- Attribute Lazy Loading And Jackson Serialization


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.