EAGER fetching is a code smell when using JPA and Hibernate

Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this post, I’ll explain why you should prefer query-based fetching instead of global fetch plans. Fetching 101 Hibernate defines four association retrieving strategies: Fetching Strategy Description Join The association is OUTER JOINED in the original SELECT statement Select An additional SELECT statement is used to retrieve the associated entity(entities) Subselect An additional SELECT statement is used to retrieve the whole associated collection. This mode is meant for to-many associations Batch An… Read More

Why you should always check the SQL statements generated by Criteria API

Introduction Criteria API is very useful for dynamically building queries, but that’s the only use case where I’d use it. Whenever you have a UI with N filters that may arrive in any M combinations, it makes sense to have an API to construct queries dynamically, since concatenating strings is always a path I’m running away from. The question is, are you aware of the SQL queries your Criteria API generates behind the scenes? I’ve been reviewing many such queries lately, and I’ve been struck by how easy it is to get… Read More