Java Reference
In-Depth Information
A Thin Solution?
One of the benefits often touted for Hibernate is that it is a “thin” solution. The problem with
this description is that it is very much an informal term, so it doesn't really tell you anything
about what attributes Hibernate has that could categorize it as thin.
Hibernate does not require an application server to operate (while EJBs do). It is therefore
applicable in client-side applications in which EJBs are entirely inappropriate. So from this
point of view, it is perhaps thin.
On the other hand, Hibernate makes use of an inordinate number of supporting libraries.
So, if you are considering download times and disk space for an applet, Hibernate will look
somewhat obese; though in these days of fast connections and copious disk space, it is
unlikely to be a deciding factor.
A Hibernate Hello World Example
Listing 1-4 shows how much less boilerplate is required with Hibernate than with the JDBC
approach from Listing 1-3.
Listing 1-4. The Hibernate Approach to Retrieving the POJO
public static List getMessages(int messageId)
throws MessageException
{
SessionFactory sessions =
new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List list = session.createQuery("from Message").list();
tx.commit();
tx = null;
return list;
} catch ( HibernateException e ) {
if ( tx != null ) tx.rollback();
log.log(Level.SEVERE, "Could not acquire message", e);
throw new MotdException(
"Failed to retrieve message from the database.",e);
} finally {
session.close();
}
}
Search WWH ::




Custom Search