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 }
Enabling a query cache:
To use this feature, we will first need to enable the query cache by adding the following tag in the configuration file:
<property name="hibernate.cache.use_query_cache">true</property>
How to do it…
Here...