This article was last updated on : November 4th, 2016 at 3:47 am

Hibernate configuration – 3 ways to create Hibernate SessionFactory, Session Object

In this tutorial I will explain you the mostly used three basic ways to do hibernate configuration. After doing proper hibernate configuration we can easily get hibernate session object.

Hibernate is a ORM tool. Using Hibernate we can insert, update and delete records from Database. Before inserting, updating or deleting any object from Database using Hibernate, we need to tell Hibernate about our application settings or configuration. For example, What kind of database are we using? How do we connect to the database? What are the objects we want to persist?

You can do Hibernate configuration in three ways:

Idea 1. Hibernate configuration programmatically using API:

While configuring Hibernate programmatic way we use Hibernate provided API to load the hbm.xml files, load the database driver and specify the database connection details.

We will demonstrate this programmatic configuration by taking one java standalone example. Here is one example to configure Hibernate session by using hibernate API.

Eclipse Project Image for this example:

Hibernate configuration using hibernate API

Hibernate Configuration Workspace

Image shown above is eclipse project image for the programmatic configuration of hibernate. You can create one simple java project with hibernate capabilities as shown in the image. Here our java project name is “programmaticallyHibernateMySql”.

HibernateUtil.java


package com.hibernate.dbConnection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory getSessionFactory() {
// create configuration using hibernate API
Configuration configuration = new Configuration();
configuration.setProperty("connection.driver_class",
"com.mysql.jdbc.Driver");
configuration.setProperty("hibernate.connection.url",
"jdbc:mysql://localhost/test");
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "mysql");
return configuration.buildSessionFactory();
}
public static void main(String[] args) {
// getting session factory
SessionFactory sessionFactory = getSessionFactory();
System.out
.println("Session factory object created : " + sessionFactory);
Session session = sessionFactory.openSession();
try {
System.out.println("Session object created : " + session);
// We can use this session object for doing CRUD (inserting,
// updating and deleting rows)
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}

Output :

Once we will run this java program the output of the code will look like below give texts. In case your configuration is incorrect you will get an exception screen.

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Session factory object created : [email protected]
Session object created : SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])

This [email protected] may be different for you.
For me also it is changing everytime for new ouput.

Idea 2. Hibernate configuration using XML :

XML configuration is considered to be the best and standard way to configure hibernate session.
We use to provide database connection parameters, database driver load details, objects to be persist details in hibernate.cfg.xml.
Please read :   “how to create Hibernate Session Object using hibernate.cfg.xml”.

Idea 3. Hibernate configuration using Properties file :

In this method we use .properties file to configure hibernate session. We provide all the database credentials in a properties file. This is similar to the XML configuration but uses a .properties file. The default name is hibernate.properties.

Share this post:

Recent Posts

One Comment

  1. Avatar for Tarun Singh
    Duanemccune says:

    Your style is so unique in comparison to other folks I’ve
    read stuff from. Many thanks for posting when you’ve got the opportunity, Guess
    I will just book mark this blog.

Leave a Comment