How to cache non-existing entity fetch results with JPA and Hibernate

Introduction Sergey Chupov asked me a very good question on Twitter: My use-case is a search by complex primary key, but the performance test still showed a degrade. I'm now using a query cache instead, which helps, but it doesn't look right to have a separate query for the search by PK. So I'm wondering if there's a better approach — Sergey Chupov (@scadgek) December 29, 2017 In this article, I’m going to show you how to cache null results when using JPA and Hibernate.