views:

539

answers:

2

What is detached persistance and transient object in hibernate ?

please clearify me with example..

Thankx in advance..

+1  A: 

See here.

Rachel
You wrote this answer in 8 minutes? Wow.
Tomislav Nakic-Alfirevic
upon session.close won't it remove all objects those were persistance in the session scope?
org.life.java
Yes it will remove so whenever you close a session persistent state of an object is converted to Detached state.
Rachel
ok thankxxxxx man..........
org.life.java
Pascal Thivent
I've pushed that back as a link. If there is any *original* content in your answer, feel free to edit **that** back in. But **don't** copy paste from external sites.
Marc Gravell
Thanks for updating with the link. I thought providing content for the OP would be beneficial.
Rachel
A: 

A new instance of a a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

Person person = new Person();
person.setName("Foobar");
// person is in a transient state

A persistent instance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long) session.save(person);
// person is now in a persistent state

Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).

All this is clearly explained is the whole Chapter 10. Working with objects of the Hibernate documentation that I'm only paraphrasing above. Definitely a must read.

Pascal Thivent