CHAPTER 9 Working with objects the end of

CHAPTER 9 Working with objects the end of a unit of work, the persistence context closes. What is the state of the object you re holding a reference to now, and what can you do with it? We refer to these objects as detached, indicating that their state is no longer guaranteed to be synchronized with database state; they re no longer attached to a persistence context. They still contain persistent data (which may soon be stale). You can continue working with a detached object and modify it. However, at some point you probably want to make those changes persistent in other words, bring the detached instance back into persistent state. Hibernate offers two operations, reattachment and merging, to deal with this situation. Java Persistence only standardizes merging. These features have a deep impact on how multitiered applications may be designed. The ability to return objects from one persistence context to the presentation layer and later reuse them in a new persistence context is a main selling point of Hibernate and Java Persistence. It enables you to create long units of work that span user think-time. We call this kind of long-running unit of work a conversation. We ll get back to detached objects and conversations soon. You should now have a basic understanding of object states and how transi tions occur. Our next topic is the persistence context and the management of objects it provides. 9.1.2 The persistence context You may consider the persistence context to be a cache of managed entity instances. The persistence context isn t something you see in your application; it isn t an API you can call. In a Hibernate application, we say that one Session has one internal persistence context. In a Java Persistence application, an EntityManager has a persistence context. All entities in persistent state and managed in a unit of work are cached in this context. We walk through the Session and EntityManager APIs later in this chapter. Now you need to know what this (internal) persistence context is buying you. The persistence context is useful for several reasons: Hibernate can do automatic dirty checking and transactional write-behind. Hibernate can use the persistence context as a first-level cache. Hibernate can guarantee a scope of Java object identity. Hibernate can extend the persistence context to span a whole conversation. All these points are also valid for Java Persistence providers. Let s look at each feature.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.