In this article, we are going to see the best way to initialize LAZY proxies and collections when using JPA and Hibernate.
I decided to write this article because there are way too many resources available on the Internet that mislead the reader into using awkward and inefficient practices.
Continue reading “The best way to initialize LAZY entity and collection proxies with JPA and Hibernate”
The JPA lazy loading mechanism can either be implemented using Proxies or Bytecode Enhancement so that calls to lazy associations can be intercepted and relationships initialized prior to returning the result back to the caller.
Initially, in JPA 1.0, it was assumed that Proxies should not be a mandatory requirement, and that’s why
@OneToOne associations use an EAGER loading strategy by default. However, EAGER fetching is bad for performance so it’s better to use the
FetchType.LAZY fetching strategy for all association types.
In this article, we are going to see how the proxy mechanism works and how you can unproxy a given Proxy to the actual entity.
Continue reading “How does a JPA Proxy work and how to unproxy it with Hibernate”
When fetching an entity, all attributes are going to be loaded as well. This is because every entity attribute is implicitly marked with the
@Basic annotation whose default fetch policy is
However, the attribute fetch strategy can be set to
FetchType.LAZY, in which case the entity attribute is loaded with a secondary select statement upon being accessed for the first time.
@Basic(fetch = FetchType.LAZY)
This configuration alone is not sufficient because Hibernate requires bytecode instrumentation to intercept the attribute access request and issue the secondary select statement on demand.
Continue reading “The best way to lazy load entity attributes using JPA and Hibernate”
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.
Continue reading “How to lazy load entity properties with Hibernate”