Book Image

Mastering Hibernate

Book Image

Mastering Hibernate

Overview of this book

Hibernate has been so successful since its inception that it even influenced the Java Enterprise Edition specification in that the Java Persistence API was dramatically changed to do it the Hibernate way. Hibernate is the tool that solves the complex problem of Object Relational Mapping. It can be used in both Java Enterprise applications as well as .Net applications. Additionally, it can be used for both SQL and NoSQL data stores. Some developers learn the basics of Hibernate and hit the ground quickly. But when demands go beyond the basics, they take a reactive approach instead of learning the fundamentals and core concepts. However, the secret to success for any good developer is knowing and understanding the tools at your disposal. It’s time to learn about your tool to use it better This book first explores the internals of Hibernate by discussing what occurs inside a Hibernate session and how Entities are managed. Then, we cover core topics such as mapping, querying, caching, and we demonstrate how to use a wide range of very useful annotations. Additionally, you will learn how to create event listeners or interceptors utilizing the improved architecture in the latest version of Hibernate.
Table of Contents (16 chapters)

Managing the cache


Besides configuring the cache in your setting files, you can further modify the cache behavior in the code. This is important because in some cases, you would like to bypass the cache completely or force the cache to expire. We will discuss these cases here.

Remove cached entities

In some cases, you want to force the eviction of a cached entity because you may know at some of point of your program execution you will end up with stale data. You can evict entities from both the first-level cache and the second-level cache.

The API to evict entities from the first-level cache is through the session object, that is, session.evict(). You should note that if the cached entity has been modified during the session, the changes would not be pushed to the database when the session is flushed.

Similarly, the second-level cache provides the interface to evict a cached entity. The Cache class offers this API. (This used to be under SessionFactory, but it has been moved, so you first have...