Newsletter |
Steps To Use Hibernate In Any Java Application
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 .:: | ||
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.
very much helpful to beginners
Hi sir,
explain hibernate tools with procedure(screen shots).
is it possible to develop hibernate application without configuration file?(pls provide example)
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…
@Ganesh
Got you, it was changed now, good observation, thank you 🙂
Great Work!!! Keep it up.
Very useful information for beginners….
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…..
its helpful for beginners thanku
BEST HIBERNATE TUTORIAL ON THE WEB……………
BEST OF LUCK…….
KEEP GOING JAVA4S……
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
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:)
nice….
thnx lot bro
It’s very usefull
Thanks
Kamta
Superb explanation!!! ThankS Java4s Team
Excellent .Superb explanation!!! ThankS Java4s Team
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!
This’s a awesome tutorial. Many thanks Java4s team.
Hi Java4s Team,
Can u post hibernate fetching strategy tutorial.
Thanks
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.
when will hbm file load
Nice tutorials,this is so helpful for hibernate begginers.
It is excellent ya.Thanks for updating the Hibernate flow of execution.
It is exactly correct.
Nice tutorial………
Nothing to say more its god gift for me…
good explanation
it’s very helpful for me….
More than class notes…
Thanks for java4s.com
Good tutorial for beginners with step by step explanation. Unlike other tutorials ,you are giving jumping into the code directly.Great work!!
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??
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.
Easy and simple to learn the basic concept of
Hibernate.
Thanks for your efforts.
Hi Java4s team
Thanks for this simple explanation.
I also want some deeply explanation.
Thanks a lot
My question is:
How database connection is opened internally when session object comes?
Thank you so much for this beautiful explanation.
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.
Great ! thank you.
Really great experience of learning!
ur a killer in explanation bro…try to give more tutorials on more technologies
Great, explained nicely in simple language.
thank you
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?