How to use Ehcache as Hibernate’s 2nd Level Cache

How to use Ehcache as Hibernate’s 2nd Level Cache

Using JPA’s and Hibernate’s 2nd level cache can improve the performance of your persistence layer. It acts as a session-independent entity store. Hibernate uses it whenever you call the find method on your EntityManager or traverse an association to fetch a cached entity. Instead of executing a database query, Hibernate gets the entity object from…

Java Text Blocks – Using Multiline Strings with Hibernate & JPA
|

Java Text Blocks – Using Multiline Strings with Hibernate & JPA

JPA and Hibernate require you to write lots of Strings. You use them to create ad-hoc and named queries with JPQL, to define native SQL queries and to specify the fetching behavior in an EntityGraph. Until Java 13, the lack of multiline Strings in Java made all of these tasks either annoying to implement or…

Your 2 best options to fix Hibernate’s MultipleBagFetchException

Your 2 best options to fix Hibernate’s MultipleBagFetchException

You probably learned that you should use FetchType.LAZY for all of your associations. It ensures that Hibernate initializes an association when you use it and doesn’t spend any time getting data you don’t need. Unfortunately, this introduces a new issue. You now need to use a JOIN FETCH clause or an EntityGraph to fetch the…

LazyInitializationException – What it is and the best way to fix it
|

LazyInitializationException – What it is and the best way to fix it

The LazyInitializationException is one of the most common exceptions when working with Hibernate. There are a few easy ways to fix it. But unfortunately, you can also find lots of bad advice online. The proclaimed fixes often replace the exception with a hidden problem that will cause trouble in production. Some of them introduce performance…