hibernate

Create Hibernate SessionFactory example

In this example we shall show you how to create a new SessionFactory example in Hibernate. To create a new SessionFactory example in Hibernate one should perform the following steps:

  • Create a new Configuration, that allows the application to specify properties and mapping documents to be used when creating a SessionFactory. Usually an application will create a single Configuration, build a single instance of SessionFactory and then instantiate Sessions in threads servicing client requests.
  • With configure() API method use the mappings and properties specified in an application resource named hibernate.cfg.xml. Then, with buildSessionFactory() instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configuration after building the SessionFactory will not affect it.

In the code snippets that follow, you can see  the CreateHibernateSessionFactoryExample Class that applies all above steps and the hibernate.cfg.xml file, that holds all configuration for Hibernate.

package com.javacodegeeks.snippets.enterprise;

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

public class CreateHibernateSessionFactoryExample {
	
	@SuppressWarnings("unused")
	private static SessionFactory sessionFactory;
	
	public static void main(String[] args) throws Exception {
		
		sessionFactory = new Configuration().configure().buildSessionFactory();
		
	}

}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

  
<hibernate-configuration>
    <session-factory>

  <!-- JDBC connection settings -->

  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

  <property name="connection.url">jdbc:mysql://localhost/companydb</property>

  <property name="connection.username">jcg</property>

  <property name="connection.password">jcg</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.NoCacheProvider</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">update</property>

  
    </session-factory>
    
</hibernate-configuration>

 
This was an example of how to create a new SessionFactory example in Hibernate.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button