View RSS Feed

My Java Tips

Hibernate - SessionFactory

Rate this Entry
by , 10-29-2011 at 07:44 PM (1034 Views)
We are writing an application that will use Hibernate to manage our application persistence object. We will need a SessionFactory which creates or open a session to talk to a database.


Creating SessionFactory is easy. We can define the configuration in hibernate.properties, hibernate.cfg.xml or create it programatically. I will present an example that will use the hibernate.cfg.xml configuration file, which is mostly use when creating Hibernate application.

Below is our session configuration files.

<pre lang="java">
<!--?xml version='1.0' encoding='utf-8'?-->
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<hibernate-configuration>
<session-factory>
<!-- JDBC connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driv er</property>
<property name="connection.url">jdbc:mysql://localhost/exampledb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool, use Hibernate internal connection pool -->
<property name="connection.pool_size">5</property>

<!-- Defines the SQL dialect used in Hiberante's application -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

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

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.No CacheProvider</property>

<!-- Display and format all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<!-- Mapping to hibernate mapping files -->
<!--mapping resource="org/test/example/hibernate/app/Label.hbm.xml"/-->
</session-factory>
</hibernate-configuration></pre>


Now we have the configuration file done, let's create a helper class that will configure and build the SessionFactory object. This helper will be used in other Hibernate example in this site.
Java Code:
package org.test.example.hibernate.app;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryHelper {
    private static final SessionFactory sessionFactory;

    static {
        try {
            /*
             * Build a SessionFactory object from session-factory configuration
             * defined in the hibernate.cfg.xml file. In this file we register
             * the JDBC connection information, connection pool, the hibernate
             * dialect that we used and the mapping to our hbm.xml file for each
             * POJO (Plain Old Java Object).
             *
             */
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.err.println("Error in creating SessionFactory object."
                + e.getMessage());
            throw new ExceptionInInitializerError(e);
        }
    }

    /*
     * A static method for other application to get SessionFactory object
     * initialized in this helper class.
     *
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Submit "Hibernate - SessionFactory" to Facebook Submit "Hibernate - SessionFactory" to Digg Submit "Hibernate - SessionFactory" to del.icio.us Submit "Hibernate - SessionFactory" to StumbleUpon Submit "Hibernate - SessionFactory" to Google

Updated 10-29-2011 at 07:47 PM by Java Tip

Tags: None Add / Edit Tags
Categories
Hibernate

Comments