Wednesday, 24 December 2014

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

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

In this tutorial we 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:

1. Hibernate Programmatic configuration 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:

programmatically hibernate session workspace image 300x257 Hibernate configuration   3 ways to create Hibernate SessionFactory, Session Object
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.
1
2
3
4
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 : org.hibernate.impl.SessionFactoryImpl@192b996
Session object created : SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])
This SessionFactoryImpl@192b996 may be different for you.
For me also it is changing everytime for new ouput.

2. XML configuration:

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”.

3. Properties file configuration:

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

0 comments

Post a Comment