Newsletter

Steps To Use Hibernate In Any Java Application

Hibernate » on May 25, 2011 { 41 Comments } By Sivateja

Hello mates, this is the exact flow of any hibernate application. so you must put little more concentration while you are reading this post, to understand better.

Whether the java application will run in the server or without server, and the application may be desktop or stand alone, swing, awt, servlet…what ever, but the steps are common to all.

In order to work with hibernate we don’t required any server as mandatory but we need hibernate software (.jar(s) files).

Follow The Steps:

1. Import the hibernate API, they are many more, but these 2 are more than enough…

import org.hibernate.*;
import org.hibernate.cfg.*;

2. Among Configuration, Mapping xml files, first we need to load configuration xml, because once we load the configuration file, automatically mapping file will be loaded as we registered this mapping xml in the configuration file.

So to load configuration xml, we need to create object of Configuration class, which is given in org.hibernate.cfg.*;  and we need to call configure() method in that class, by passing xml configuration file name as parameter.

Eg:

Configuration cf = new Configuration();
cf.configure(“hibernate.cfg.xml”);

Here our configuration file name is your choice, but by default am have been given hibernate.cfg.xml,  so once this configuration file is loaded in our java app, then we can say that hibernate environment is started in our program.

So once we write the line_ cf.configure(“hibernate.cfg.xml”), configuration object cf will reads this xml file hibernate.cfg.xml, actually internally cf will uses DOM parsers to read the file.

Finally…

  • cf will reads data from hibernate.cfg.xml
  • Stores the data in different variables
  • And finally all these variables are grouped and create one high level hibernate object we can call as SessionFactory object.
  • So Configuration class only can create this SessionFactory object

likeSessionFactory sf =  cf.buildSessionFactory();

Actually SessionFactory is an interface not a class, and SessionFactoryImpl is the implimented class for SessionFactory, so we are internally creating object of SessionFactoryImpl class and storing in the interface reference, so this SessionFactory object sf contains all the data regarding the configuation file so we can call sf as heavy weight object.

3. Creating an object of session,

  • Session is an interface and SessionImpl is implemented class, both are given in org.hibernate.*;
  • When ever session is opened then internally a database connection will be opened, in order to get a session or open a session we need to call openSession() method in SessionFactory, it means SessionFactory produces sessions.

Session session = sf.openSession();

sf = SessfionFactory object

4. Create a logical transaction

While working with insert, update, delete, operations from an hibernate application onto the database then hibernate needs a logical Transaction, if we are selecting an object from the database then we do not require any logical transaction in hibernate.  In order to begin a logical transaction in hibernate then we need to call a method beginTransaction() given by Session Interface.

Transaction tx = session.beginTransaction();

session is the object of Session Interface

5. Use the methods given by Session Interface,  to move the objects from application to database and  from database to application

session .save(s)Inserting object ‘s‘ into database
session.update(s)Updating object ‘s‘ in the database
session.load(s)Selecting object ‘s‘ object
session.delete(s)Deleting object ‘s‘ from database
  • So finally we need to call commit() in Transaction, like tx.commit();
  • As i told earlier,  when we open session a connection to the database will be created right, so we must close that connection as session. close().
  • And finally close the SessionFactory as sf.close()
  • That’s it.., we are done.

Final flow will be______________

Configuration

SessionFactory

Session

Transaction

Close Statements

 

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

41 Responses to “Steps To Use Hibernate In Any Java Application”
  1. Mohammed Vaseem says:

    Hello Java4s Team,
    You are providing tremendous by offering valuable technology tutorials.
    Please provide the list of exceptions which occur during the program development and their reasons for Struts2 and as well as for Hibernate to make ease in error tracing.
    Thanks & Regards:
    Mohammed Vaseem.

  2. srnivas sriperambuduri says:

    very much helpful to beginners

  3. madala says:

    Hi sir,
    explain hibernate tools with procedure(screen shots).
    is it possible to develop hibernate application without configuration file?(pls provide example)

  4. Ganesh says:

    Hi…
    Tutorials are nice and easy to follow. Thank you for simple explanation.

    In step 2 you said… “and we need to call configuration() method in that class” ! I think its configure() method. I guess its spelling mistake…

  5. Java4s says:

    @Ganesh

    Got you, it was changed now, good observation, thank you 🙂

  6. Ravi says:

    Great Work!!! Keep it up.

  7. Bhushan says:

    Very useful information for beginners….

  8. Arun says:

    what a golden tutorial .Nothing to say superb… . i have seen other websites only with programs but no websites with this much explanation..
    Thank u very much for JAVA4S…..

  9. nandu says:

    its helpful for beginners thanku

  10. BEST HIBERNATE TUTORIAL ON THE WEB……………

    BEST OF LUCK…….

    KEEP GOING JAVA4S……

  11. utkalsamal says:

    How we can change schema dynamically ?
    I mean to say , i want to have multiple schema inside the hibernate.cfg.xml ,So based on parameter i want to connect to that schema.please suggest me how it is possible.\
    Thank you

  12. saravanan palani says:

    Hi Java4s team,

    i was expecting these kind of explanation fortunately i reached you and got my need thanks for the tutorial and keep up the good work guys:)

  13. Arun Singh says:

    nice….
    thnx lot bro

  14. Kamta Mishra says:

    It’s very usefull
    Thanks
    Kamta

  15. Ananth105 says:

    Superb explanation!!! ThankS Java4s Team

  16. Jagadeesh says:

    Excellent .Superb explanation!!! ThankS Java4s Team

  17. sambachinta says:

    Hi, Java4s,

    Your tutorial is very good for beginners and excellent for understanding….

    Will you please explain me to hibernate general flow?

    When a request come from a client, first that request enters into………………plz help me!

  18. MaiHung says:

    This’s a awesome tutorial. Many thanks Java4s team.

  19. Susil Kumar Rout says:

    Hi Java4s Team,
    Can u post hibernate fetching strategy tutorial.
    Thanks

  20. Pankaj says:

    Sir ,
    Please explain…
    i want to use hibernate with jsp and servlet……………
    why we can not add session.serAttribute(“something”); with this session variable
    in my servlet class.

  21. nitin says:

    when will hbm file load

  22. suraj says:

    Nice tutorials,this is so helpful for hibernate begginers.

  23. It is excellent ya.Thanks for updating the Hibernate flow of execution.
    It is exactly correct.

  24. Amol says:

    Nice tutorial………

  25. ebin says:

    Nothing to say more its god gift for me…

  26. Raji says:

    good explanation

  27. it’s very helpful for me….
    More than class notes…
    Thanks for java4s.com

  28. Srilatha says:

    Good tutorial for beginners with step by step explanation. Unlike other tutorials ,you are giving jumping into the code directly.Great work!!

  29. Vaibhav says:

    Thanks to you to provide awesome hibernate tutorial for beginners.. But I didn’t understand why we are internally creating object of SessionFactoryImpl class and storing in the interface reference? Why we use Interface reference instead of Class reference? Is there any benefit to the program logic? Can you explain please??

  30. shankar says:

    i m getting a log4j error:-

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.

  31. anurag Bharti says:

    Easy and simple to learn the basic concept of
    Hibernate.
    Thanks for your efforts.

  32. Hi Java4s team
    Thanks for this simple explanation.
    I also want some deeply explanation.

  33. deep says:

    Thanks a lot

  34. Akshay says:

    My question is:
    How database connection is opened internally when session object comes?

  35. mounesh says:

    Thank you so much for this beautiful explanation.

  36. Mahendra says:

    I have one confusion, you said like in hibernate we do not need to bother about closing the connection but here you are closing the connection. Could you please explain/clarify.

  37. Sanjay says:

    Great ! thank you.

  38. praful kamble says:

    Really great experience of learning!

  39. y maheshreddy says:

    ur a killer in explanation bro…try to give more tutorials on more technologies

  40. Sarika says:

    Great, explained nicely in simple language.
    thank you

  41. Tarka says:

    Can you please update this site about how hibernate works without using any configuration files i.e xml files. How the control flows from start to end in hibernate?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.