Hibernate Lifecycle Of pojo Class Objects

Hibernate » on Jun 18, 2011 { 52 Comments } By Sivateja

Actually our POJO class object having 3 states like…

  • Transient state
  • Persistent state
  • Detached state


Transient & Persistent states:

  • When ever an object of a pojo class is created then it will be in the Transient state
  • When the object is in a Transient state it doesn’t represent any row of the database, i mean not associated with any Session object, if we speak more we can say no relation with the database its just an normal object
  • If we modify the data of a pojo class object, when it is in transient state then it doesn’t effect on the database table
  • When the object is in persistent state, then it represent one row of the database, if the object is in persistent state then it is associated with the unique Session
  • if we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session
  • if we want to move an object from persistent state into transient state then we need to delete that object permanently from the database



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

public class ClientProgram { 

	public static void main(String[] args)

		Configuration cfg = new Configuration();

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();

         // Transient state_____start
		Product p=new Product();
         // Transient state_____end

         // Persistent state_____start
		Transaction tx = session.beginTransaction();;
		System.out.println("Object saved successfully.....!!");
         // Persistent state_____end  




  • see the above client program, line numbers 16 to 19 we just loaded the object and called the corresponding setter methods, its not related to the database row
  • if you see, line number 24 we called save method in the Session Interface, means the object is now having the relation with the database
  • if we want to convert the object from Transient state to Persistentstate we can do in 2 ways
    • By saving that object like above
    • By loading object from database

If we do any modifications all the changes will first applied to the object in session cache only (Let__ we do the modifications 5 times, then 5 times we need to save the changes into the database right, which means number of round trips from our application to database will be increased, Actually if we load an object from the database, first it will saves in the cache-memory so if we do any number of changes all will be effected at cache level only and finally we can call save or update method so with the single call of save or update method the data will be saved into the database.

If we want to save an object into database then we need to call any one of the following 3 methods

  • save()
  • persist()
  • saveOrUpdate()

i will explain about persist, saveOrUpdate methods later….

If we want to load an object from database, then we need to call either load() or get() methods


One newly created object,with out having any relation with the database, means never persistent, not associated with any Session object


Having the relation with the database, associated with a unique Session object


previously having relation with the database [persistent ], now not associated with any Session

see the next sessions for the better understanding of the life cycle states of pojo class object(s) the hibernate


You Might Also Like

  ::. About the 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.


52 Responses to “Hibernate Lifecycle Of pojo Class Objects”
  1. Ambresh Sharna says:

    Explanation is very clear not doubt about it…overall it’s prove to be fine tutorial for me..i am happy to go through this site…i am looking for JQuery tutorial on this site..hope you add i can learn in better way?? Thanks Java4s

  2. Java4s says:

    @Ambresh Sharna

    Thanks for your feedback 🙂

    We are working hard to present the required tutorials, will post jQuery related articles soon.

  3. nicely explained….kinda professional way…Thanks a lot !!

  4. maruthi says:

    very good tutorial

  5. Rajendra Rawat says:

    Excellent tutorial on hibernate I ever found.

  6. It is very great tutorial & one of best for learning hibernate.
    Thanks for this.

  7. Great tutorial. But you should link posts.
    Like say, now I want to read about saveOrUpdate but you don’t have link to that post. 🙂

  8. Good Explanation About three states of POJO with program by writing comments in the program.

  9. Very Good Tutorial… Thanx a lot

  10. anji says:

    Great tutorial. i am seeing first time This tutorial. very very useful for me.

    Thanks for this.

  11. Aditya says:

    Nice explaination. Thanks 🙂

  12. Very nice article. Now i am, able to understand the role of Hibernate and how to use it. Thanks a lot.

  13. basha says:


    Your explanation is very clear.
    what is difference between load() and get()?
    And also i want learn JQuery,
    Please provide Jquery documenation in your site.
    Thank you…

  14. Jamadagni says:

    Nice article on Hibernate. simple and the best.

  15. rahul ojha says:

    super explanations each steps

  16. Chetan says:

    Nice tutorial you have given for all java bie 🙂

    I gone through on core/struts2/hibernate found explanation and example……….even u have given explanation with line no. like compiler……………..grt work u have done………….pls keep it……………could you please brief me about webservice’s specially i would like to you on REST Full WebServices………….Thanks in advance;

    Chetan Bhandari

  17. Java4s says:


    We already started posting Web Services, We will try to finish as soon as we can. Mean while you can start learning RESTful Web Services @


  18. Mahesh Ghatage says:

    I clearly understand about the object states thanks java4s

  19. Tulasi says:

    So Nice

  20. Yogita says:

    Very awesome tutorial. Very easy to understand.
    Thanks a lot.

  21. lokesh says:

    HIi Siva,

    i need a help,
    we have a list as field to store it in data base,
    for this we have to use hibernate annotations mapping class(no hibernate mapping file should be used), pojo class object contains some fields as List, those fileds will be inserted into db as string by mapping class. if possible for you, give an example

  22. Ram says:

    Great Explanation…….. 🙂

    thousands of thanks to JAVA4S team.

  23. Ramanath says:

    That is a simple way to explain lifecycle of an object in Hibernate. Good Job!! Keep up the good things!!

  24. Shreyas+Deshpande says:

    Hi Siva,
    Overall nice clarification, thanks for it.
    but I am a bit confused over a below thing.
    – when an object is said to be in PERSISTENT state? when it is in session(factory.openSession()) or when it is in transaction (session.beginTransaction())?
    please reply.
    thanks in advance

  25. ramesh says:


    how many types of session’s in hibernate?
    this quetion I was faced in capgemini,
    I given state’s of session,is it right one?

    replay me right answer

  26. sadeesh says:

    Superb …well explained !!!!!!

  27. abhishyam says:

    Good tutorial,
    very good explanation of each ans every concept

  28. Anirban says:

    Nicely concepts explained, concise too.
    Great work. Thanks. It really helped.

  29. Ankush says:

    Very Nice Tutorial sir…………..

  30. Ankush Dapke says:

    Very Nice Explanation……………

  31. Pradeep says:

    Thanks teja………very helpfull

  32. Veranga says:

    Good . Well explained.

  33. ajay tiwari says:

    nice tutorial
    all matter well explained

  34. Srushti says:

    Too good Explanation. Short and clear, and yes really helpful.

  35. Ajit says:

    explanation is very good and easy to understand also.

  36. kapilnagar says:

    very nice tutorial for java concept

  37. yogesh rathore says:

    very Good tutorial
    very good explanation of each ans every concept


  38. srinu says:

    best Tutorial for Explanation
    please provide JavaScript tutorial


  39. pradeep says:

    hello sir,
    can i use session tag in the cfg class

  40. Murali says:

    Hi…Very Superrrrrrrrbb tutorial….Thank you so much…

    And I need a small clarification, loading object from database means Detached State to Persistence State or Transient State to Persistence State??

    Detached State means object present in the database table right or anywhere else?

  41. Manjunath MV says:

    Very good Explanation

  42. jagadish says:

    very Good Explanation…Thank you so much

  43. Madhuri says:

    Very good explanation…..thanks alot .. .to admin and members….its very use full to me, please add jQuery n JavaScript……

  44. manish says:

    please provide full tutorials link for hibernate and spring mvc with spring .
    I shall be thankful to you for this.

  45. its very clearly talking and explanation very nice this tutotrial

  46. Guru says:

    Nice tutorials..
    kindly provide 4+ years experience interview questions, discssions and programs too.

    Thank you very much….

  47. jithu says:

    very nice…..

    Thank you very much

  48. Bhuvan says:

    Nice woah.,Information is helpful

  49. Vaishali says:

    Best tutorial for hibernate! Very well explained with examples. I was not able to get the concept from other sites but u people have really explained perfectly.

  50. Sanjay says:

    Mind blowing .. thanks a lot !!

  51. Shivaraj Hosur says:

    Very good and clear Explanation..Thank you


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.