Java Reference
In-Depth Information
Persistent objects exist in the database, and Hibernate manages the persistence for per-
sistent objects. We show this relationship between the objects and the database in Figure 4-2.
If fields or properties change on a persistent object, Hibernate will keep the database repre-
sentation up-to-date.
Figure 4-2. Persistent objects are maintained by Hibernate.
Detached objects have a representation in the database, but changes to the object will not
be reflected in the database, and vice versa. This temporary separation of the object and the
database is shown in Figure 4-3. A detached object can be created by closing the session that
it was associated with, or by evicting it from the session with a call to the session's evict()
method.
Figure 4-3. Detached objects exist in the database but are not maintained by Hibernate.
In order to persist changes made to a detached object, the application must reattach it to a
valid Hibernate session. A detached instance can be associated with a new Hibernate session
when your application calls one of the load() , refresh() , merge() , update() , or save() methods
on the new session with a reference to the detached object. After the call, the detached object
would be a persistent object managed by the new Hibernate session.
Versions prior to Hibernate 3 had support for the Lifecycle and Validatable interfaces.
These allowed your objects to listen for save , update , delete , load , and validate events using
methods on the object. In Hibernate 3, this functionality moved into events and interceptors,
and the old interfaces were removed.
Entities, Classes, and Names
Entities represent Java objects with mappings that permit them to be stored in the database.
The mappings indicate how the fields and properties of the object should be stored in the
database tables. However, it is possible that you will want objects of a particular type to be
represented in two different ways in the database. In this case, how does Hibernate choose
which to use?
Search WWH ::




Custom Search