Java Reference
In-Depth Information
B Boilerplate
code
SessionFactoryUtils.getSession(
sessionFactory, true);
try {
C Code that loads
the object
return (PendingOrder)
session.load(PendingOrder.class,
new Integer(pendingOrderId));
} catch (HibernateException e) {
throw SessionFactoryUtils.
bbbbbb convertHibernateAccessException(e);
} finally {
SessionFactoryUtils.releaseSession(session,
sessionFactory);
}
}
D More
boilerplate
public PendingOrder createPendingOrder() {
}
}
Let's take a closer look at this listing:
B
The first piece of boilerplate code calls SessionFactoryUtils.getSession() to get
a Session for the specified SessionFactory . This method will return the Session
that has been opened and bound to the thread by a Spring AOP interceptor or fil-
ter. If there isn't an existing Session , the true argument tells getSession() to
open a new Session . This feature enables the repository to work both inside the
application and in a unit-testing environment.
C
D
The Session.load() method loads the specified object.
The second piece of boilerplate code consists of a catch clause and a finally clause.
The catch clause calls SessionFactoryUtils.convertHibernateAccessException()
to convert the Hibernate-specific HibernateException to a generic Spring data
access exception. A Spring data access exception is an unchecked exception that
enables the higher levels of the application to treat all data access exceptions uni-
formly. The finally clause calls SessionFactoryUtils.releaseSession() , which
closes the Session if it was opened by the call to SessionFactoryUtils.getSes-
sion() . The releaseSession() method does nothing if getSession() returned an
already open Session .
As you can see, in addition to the single call to Session.load() there are several
lines of boilerplate code to manage the Session and map the HibernateException .
 
Search WWH ::




Custom Search