Newsletter |
Hibernate Lifecycle Of pojo Class Objects
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
Example____ ClientProgram.java
import org.hibernate.*; import org.hibernate.cfg.*; public class ClientProgram { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); // Transient state_____start Product p=new Product(); p.setProductId(101); p.setProName("iPhone"); p.setPrice(25000); // Transient state_____end // Persistent state_____start Transaction tx = session.beginTransaction(); session.save(p); System.out.println("Object saved successfully.....!!"); tx.commit(); // Persistent state_____end session.close(); factory.close(); } }
Note:
- 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
Transient:
One newly created object,with out having any relation with the database, means never persistent, not associated with any Session object
Persistent:
Having the relation with the database, associated with a unique Session object
Detached:
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 .:: | ||
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 soon..so i can learn in better way?? Thanks Java4s
@Ambresh Sharna
Thanks for your feedback 🙂
We are working hard to present the required tutorials, will post jQuery related articles soon.
nicely explained….kinda professional way…Thanks a lot !!
nice tutorial…
very good tutorial
Excellent tutorial on hibernate I ever found.
It is very great tutorial & one of best for learning hibernate.
Thanks for this.
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. 🙂
Good Explanation About three states of POJO with program by writing comments in the program.
Very Good Tutorial… Thanx a lot
Great tutorial. i am seeing first time This tutorial. very very useful for me.
Thanks for this.
Nice explaination. Thanks 🙂
Very nice article. Now i am, able to understand the role of Hibernate and how to use it. Thanks a lot.
Hi,
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…
Nice article on Hibernate. simple and the best.
super explanations each steps
thanks
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;
Regards:
Chetan Bhandari
@Chetan,
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 @ https://www.java4s.com/web-services/
-Siva
I clearly understand about the object states thanks java4s
So Nice
Very awesome tutorial. Very easy to understand.
Thanks a lot.
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
Great Explanation…….. 🙂
thousands of thanks to JAVA4S team.
That is a simple way to explain lifecycle of an object in Hibernate. Good Job!! Keep up the good things!!
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
Hi,
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?
else
replay me right answer
Superb …well explained !!!!!!
Good tutorial,
very good explanation of each ans every concept
Nicely concepts explained, concise too.
Great work. Thanks. It really helped.
Very Nice Tutorial sir…………..
Very Nice Explanation……………
Thanks teja………very helpfull
Good . Well explained.
nice tutorial
all matter well explained
Too good Explanation. Short and clear, and yes really helpful.
explanation is very good and easy to understand also.
very nice tutorial for java concept
very Good tutorial
very good explanation of each ans every concept
Thanks
best Tutorial for Explanation
please provide JavaScript tutorial
Thanks
hello sir,
can i use session tag in the cfg class
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?
Very good Explanation
very Good Explanation…Thank you so much
Very good explanation…..thanks alot .. .to admin and members….its very use full to me, please add jQuery n JavaScript……
please provide full tutorials link for hibernate and spring mvc with spring .
I shall be thankful to you for this.
its very clearly talking and explanation very nice this tutotrial
Nice tutorials..
kindly provide 4+ years experience interview questions, discssions and programs too.
Thank you very much….
very nice…..
Thank you very much
Nice woah.,Information is helpful
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.
Mind blowing .. thanks a lot !!
Very good and clear Explanation..Thank you