Book Image

Java Hibernate Cookbook

By : Yogesh Prajapati, Vishal Ranapariya
Book Image

Java Hibernate Cookbook

By: Yogesh Prajapati, Vishal Ranapariya

Overview of this book

This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application. We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.
Table of Contents (15 chapters)
Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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...