-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Object States clarification.
PostPosted: Wed Feb 11, 2009 4:51 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 12:31 am
Posts: 21
Hibernate: 3.3.1.GA
Annotation 3.4.0.GA
EntityManager 3.4.0.GA

Hi,

I'm still quite confused about object states after reading a hibernate book.

When a new object is created and is not managed by a persistent context, we call it transient. Fine.

When transient object becomes managed by a persistent context WHILE the transaction is active, the object is said to be persistent right?

When the active transaction is committed, the previous persistent object is now said to be detached, right?

Now, if the session is closed by calling mySession.close() or myEntity.close(), does that detached object return to transient state?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:09 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
This is simple -

Transient - Object not known to persistence context and not representing any row of any table in database.
for example -
---Newly Created object.

Persistent - Object known to persistence context and represent (or scheduled to represent ) a row of some table in database.
for example -
-- after calling save method (now persistence context know to object ) on some object, but insert statement is not flushed yet (mean it is scheduled to represent to a row in database).

Detached - Object is not Known to persistence context but it represent some row of some table in database.
for example -
-- after call of evict method (now object will detached to persistence context) , but identifier property of object still represent to a row of table (mapped table for the class of object) in database.[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:35 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 12:31 am
Posts: 21
Sorry, it's still quite blurry.

Code:
Detached - Object is not Known to persistence context but it represent some row of some table in database.


Does that mean that a transient object can also, by accident, be a detached object? (Like for instance, you created a new object and assigned manually its properties and ID that coincidentally represents a row in the database.)

Or more accurately, is it possible to create a detached object manually without loading it first from the persistent context (without the object going to a persistent state), like creating a new object instance and assign it an ID that represents a row in the DB?

What is the new state of a persistent object when the transaction is committed BUT the session hasn't closed yet? (after calling tx.commit() and before calling mySession.close() )

What is the new state of a persistent object after the transaction is committed AND the session has closed? (after calling tx.commit() and after calling mySession.close() )

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 6:35 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Hi unrealdummy,

About your concern
Quote:
Does that mean that a transient object can also, by accident, be a detached object? (Like for instance, you created a new object and assigned manually its properties and ID that coincidentally represents a row in the database.)


Yes hibernate will treat a transient object as detached object if -
1. you assigned a value to identifier property of the object AND
2. Hibernate could not able to fine the object in persistence context AND
2. In the mapping of that class, you gave responsibility to Hibernate (by using sequence, increment etc.) to assign identifier property to object.

About -
Quote:
What is the new state of a persistent object when the transaction is committed BUT the session hasn't closed yet? (after calling tx.commit() and before calling mySession.close() )

this will be the Persistence state of object.

About -
Quote:
What is the new state of a persistent object after the transaction is committed AND the session has closed? (after calling tx.commit() and after calling mySession.close() )

This will be the Detached state of Object.

For more information read thoroughly chapter 9 of
Quote:
JAVA Persistence With Hibernate.
.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 8:20 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 12:31 am
Posts: 21
Thanks parmendratyagi,

That cleared up the cloud, so turns out it depends upon the ID of an object to determine if it's a transient or a detached object. I'll keep that three things in mind.

I thought a persistent object is an object in which persistent context has a reference and is active, and that persistent context is active WHEN a unit of work begins but hasn't ended yet (i.e. between transaction.begin() and transaction.end() ), turns out, transaction duration has nothing to do with it, but it has to do with the duration of the session, between openSession() and mySession.close()

One last question, so, does this imply that every new session, like after invoking openSession(), it's persistent context cache is empty and that persistent context has no reference to any persistent objects?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 12:48 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 12:31 am
Posts: 21
Quote:
One last question, so, does this imply that every new session, like after invoking openSession(), it's persistent context cache is empty and that persistent context has no reference to any persistent objects?


Yes


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 1:23 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Quote:
One last question, so, does this imply that every new session, like after invoking openSession(), it's persistent context cache is empty and that persistent context has no reference to any persistent objects?



Yes. Actually a persistence context is created as part of session creation and associate with newly create session. So at that time, persistence context (which work like first lever of cache for session) will be empty. A persistence context can't share across the session, this is private to a session.

There is different way to clean the persistence context. Like when you call session.clear()- what does it mean. Actually it clean session's persistence context . so all the object which was known to the persistence context will change it state form Persistence to Detach.

Persistence context is actually responsible for "Dirty Checkin".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.