Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly.
Hypersistence Optimizer is that tool!
One of my readers bumped into the JSON mapping post and asked me if we can fetch the JSON properties lazily. This post demonstrates how easily this can be done when using Hibernate as a JPA provider.
As I previously explained, EAGER fetching is a code smell and loading associations eagerly is very detriment to application performance. However, it’s not just associations that we must be careful about. Basic entity properties may also cause performance issues as well. In this post, I’m going to show you how you can fetch entity properties lazily.
By default, Hibernate loads all properties eagerly. So if your entity has many properties or the underlying columns are relatively large, there is going to be a considerable performance impact upon fetching the entity in question. While associations can be loaded lazily using runtime-generated proxies, for basic properties we need bytecode enhancement.
To enhance our entities with basic property lazy loading capabilities, the enableLazyInitialization configuration property must be explicitly set to true:
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
Fetching entity properties lazily comes in handy whenever we deal with BLOB, CLOB, BINARY, TEXT, and JSON column types. By default, all lazy properties are initialized together, but this behavior can be customized using @LazyGroup, as it will be explained in a future post.