lishman levelup
«previous  next»

Getting Started

Hibernate Session

Java Persistence API
Hibernate is also JPA compatible. EntityManager is the JPA equivalent of the native Hibernate Session class. JPA will be added to level up in the future.
The Session object is the main runtime interface between a Java application and Hibernate. It offers create, read and delete operations for instances of mapped entity classes.

Session Factory

A Session is obtained from a SessionFactory which can be obtained from the HibernateUtil helper class shown below. This is a common pattern for Hibernate startup in non-Java EE applications.
public class HibernateUtil {

  private static SessionFactory sessionFactory;

  static {
    try {

      sessionFactory =
        new AnnotationConfiguration()

    } catch (Throwable e) {
      System.err.println("Initial SessionFactory creation failed." + e);
      throw new ExceptionInInitializerError(e);

  public static SessionFactory getSessionFactory() {
    return sessionFactory;

  public static void shutdown() {
sessionFactory is a static singleton which is instantiated once during startup. This allows multiple Session objects to be created using a single SessionFactory.
Session session = HibernateUtil.getSessionFactory().getCurrentSession();


When HibernateUtil calls the configure() method, a configuration file named hibernate.cfg.xml is loaded from the root of the classpath. Here is an example of a hibernate.cfg.xml file which uses an Oracle 10g database:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"


    <!-- Database dialect -->
    <property name="dialect">

    <!-- Database connection settings -->
    <property name="connection.provider_class">
    <property name="connection.driver_class">
    <property name="connection.url">
    <property name="connection.username">lish</property>
    <property name="connection.password">secret</property>

    <!-- C3P0 connection pool -->
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50 </property>
    <property name="hibernate.c3p0.idle_test_period">3000 </property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Print all generated SQL to the console -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>

    <!-- Mapped entities -->
    <mapping class=""/>

The dialect property tells Hibernate which variation of SQL to use when talking to the database specified in the connection settings.

C3P0 is a an open source database connection pool which comes bundled with Hibernate.

A list of mapping elements specifies which annotated entity classes are managed by Hibernate in this application.