Working with a query cache

Hibernate supports a useful feature that actually helps to improve the performance of the application by reducing the processing time. The feature we are talking about here is called query cache.

Hibernate caches the query result, which is frequently used. This feature is only useful if the same queries are executed frequently.

Getting ready

To understand the query cache, we will use the Employee POJO that was created in the previous recipe entitled Working with a first-level cache. We also need to modify hibernate.cfg.xml to enable the query cache feature:

Source file: Employee.java

@Entity
public class Employee {

  @Id
  @GeneratedValue
  private long id;

  @Column(name = "name")
  private String name;
  
  // getters and setters

}

Get Java Hibernate Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.