The persistence lifecycle When interacting with the persistence (Geocities web hosting)
The persistence lifecycle When interacting with the persistence mechanism in that way, it s necessary for the application to concern itself with the state and lifecycle of an object with respect to persistence. We refer to this as the persistence lifecycle: the states an object goes through during its life. We also use the term unit of work: a set of operations you consider one (usually atomic) group. Another piece of the puzzle is the persistence context provided by the persistence service. Think of the persistence context as a cache that remembers all the modifications and state changes you made to objects in a particular unit of work (this is somewhat simplified, but it s a good starting point). We now dissect all these terms: object and entity states, persistence contexts, and managed scope. You re probably more accustomed to thinking about what statements you have to manage to get stuff in and out of the database (via JDBC and SQL). However, one of the key factors of your success with Hibernate (and Java Persistence) is your understanding of state management, so stick with us through this section. 9.1.1 Object states Different ORM solutions use different terminology and define different states and state transitions for the persistence lifecycle. Moreover, the object states used internally may be different from those exposed to the client application. Hibernate defines only four states, hiding the complexity of its internal implementation from the client code. The object states defined by Hibernate and their transitions in a state chart are shown in figure 9.1. You can also see the method calls to the persistence manager API that trigger transitions. This API in Hibernate is the Session. We discuss this chart in this chapter; refer to it whenever you need an overview. We ve also included the states of Java Persistence entity instances in figure 9.1. As you can see, they re almost equivalent to Hibernate s, and most methods of the Session have a counterpart on the EntityManager API (shown in italics). We say that Hibernate is a superset of the functionality provided by the subset standardized in Java Persistence. Some methods are available on both APIs; for example, the Session has a persist() operation with the same semantics as the EntityManager s counterpart. Others, like load() and getReference(), also share semantics, with a different method name. During its life, an object can transition from a transient object to a persistent object to a detached object. Let s explore the states and transitions in more detail.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.