How to create SessionFactory in Hibernate?

Connect with

SessionFactory in HibernateHow to create sessionFactory in Hibernate from Configuration object. 3 ways of creating Hibernate sessionFactory Example.

1. Overview of SessionFactory in Hibernate

Hibernate SessionFactory object is the first class citizen in Hibernate ORM framework for any application. Configuration Object needs information pertaining to database connection & mapping(XML or Annotations). There are several approaches to provide this information to Hibernate:

  1. Using only hibernate.cfg.xml
  2. Using

2. SessionFactory object from hibernate.cfg.xml

hibernate.cfg.xml is a standard XML file containing database connection information along with mapping information. This file needs to be placed in root of classpath of the application. I am explainting, how you configure and create sessionFactory in Hibernate example from hibernate.cfg.xml. In this example, I use mySQL for connection string.


Key points of above configuration for getting object of sessinoFactory in Hibernate are as:

  • The dialect property informs hibernate to generate database specific (MySQL here) instructions.
  • driver_class defines the database specific driver hibernate will use to make the connection.
  • username,password & URL are general connection properties, nothing special.
  • show_sql will instruct hibernate to log all the statements on console and format_sql instructs it to display properly formatted SQL.
  • mapping tag instructs hibernate to perform mapping for mentioned resources(in case of XML Mapping) or classes(in case of Annotation mapping).
  • Assuming that hibernate.cfg.xml is created for your application and placed in application classpath

Hibernate SesstionFactory(& Session) can be created from Configuration as follows:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

3. Hibernate SessionFactory Using is a standard Property file containing database connection information. In this case, you can provide mapping information directly while creating Configuration Object. Below is a sample file.

hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.url= jdbc:mysql://localhost:3306/Hibernate_Example

Assuming that file is created for your application and placed in application classpath, SesstionFactory(& Session) can be created as follows:

Configuration cfg = new Configuration().addResource("Employee.hbm.xml").addResource("Address.hbm.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();

In case you are using annotation mapping, bello code will be used for creating SessionFactory from Configuration:

Configuration cfg = new Configuration()
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();

4. Get SsessionFactory Programmatically

In case you don’t want to retain file (which may not be a good idea ), you can specify everything programatically. Its all about, how you feel comfortable but there is different aspect of it which you should consider, while configuring Hibernate sessinoFactory Object in you application.

Creating of Hibenate SessionFactaory from Configuration object, SessionFactaory example will look like:

Configuration cfg = new Configuration()
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
    .setProperty("hibernate.connection.username", "myuser");
    .setProperty("hibernate.connection.password", "mypassword")
    .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate_example")
    .setProperty("hibernate.show_sql", "true")
    .setProperty("hibernate.format_sql", "true");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();

5. Reference

If you are new in Hibernate you can visit Hello world Hibernate ExampleThanks for visiting this post for creating SessionFactaory object in Hibernate by example. You can also visit Java Hibernate ORM Tutorial Listing page for more articles on Hibernate ORM framework.
Your comments are welcome to improve this post of getting SessionFactaory object in Hibernate Java. I hope you enjoyed reading this article. Happy Learning 🙂

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *