Java Reference
In-Depth Information
base. When the session closes, the object is disconnected, thereby becoming its own trans-
ferobject.AnytimeanobjectisretrievedthroughHibernate,itbecomespartofaHibernate
session.
You retrieve a Hibernate session via a session factory. The session factory reads all the
mapping metadata, configures the framework, and performs any necessary preprocessing.
It's supposed to be instantiated only once, acting as a singleton.
Those readers who are familiar with the Spring framework (as discussed in chapter 7 )
should suddenly become interested, because managing singletons is one of the things that
Springisallabout.Anotherofitscapabilities isdeclarative transactionmanagement, which
fits in nicely too. The result is that designs in the EJB 2.x world were replaced by a com-
bination of Spring for the declarative transactions and the session factory and Hibernate for
the entity beans.
In version 3 of EJB the architecture was redesigned again to fit more closely with that used
by Spring and Hibernate. The entity beans part led to the creation of the Java Persistence
API. The JPA world uses the same concepts but labels them differently. [ 7 ] The Hibernate
Session becomes an EntityManager . The SessionFactory is an EntityMan-
agerFactory . Objects that are managed (that is, in the Hibernate session) compose a
persistence context .
7 Of course it does. Using the same terms would be too easy.
Finally, in the original Hibernate, mapping from entity classes to database tables was done
through XML files. Over time XML has become less popular and has been replaced by an-
notations. Hibernate and JPA share many annotations, which is fortunate.
It's time for an example, which will bring Spring, Hibernate, and JPA together. Chapter 7
on the Spring framework discusses Spring in some detail. Here I'll just highlight the parts
needed for the example.
To start I'll need a database. For that I'll use H2, a pure Java file- or memory-based data-
base. Spring provides an embedded database bean to make it easy to work with H2. The
relevant bean from the Spring configuration file is
 
Search WWH ::




Custom Search