Java Reference
In-Depth Information
Figure 8.4. New and deleted objects are transient. When they are saved they become persistent, and when the
session closes they become detached. Knowing the state of an object is key to understanding how it works in
Hibernate.
Any object retrieved through Hibernate—for example, by using one of the dynamic finders
or criteria queries—is placed in the persistent state and will stay in sync with the database
while it remains in that state. Newly created objects that have not yet been saved are transi-
ent, and objects that are in memory when the Hibernate session is closed are then detached.
Detached objects are no longer connected to the database.
The key question is, when is the Hibernate session created, and when is it closed? Over
time acommonpractice hasbeenestablished toscopethesessiontoasingle HTTPrequest.
This is known in the Hibernate literature as the Open Session in View (OSIV) pattern, and
it's implemented through a request interceptor. The Spring framework comes with a library
class to do this automatically, which Grails uses by default.
OSIV Bean
Grails uses an OSIV bean from Spring to scope the Hibernate session to each HTTP re-
quest. The bean intercepts incoming requests and creates the session, and then it intercepts
the outgoing responses and closes the session.
 
Search WWH ::




Custom Search