• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Identify if object is Transient, Persistent, Detached object

 
Sagar Kale
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,


I am learning hibernate.


I just read how to distinguish between detached object and transient object. for example checking the value of id if it is null then it is transient otherwise it is detached.


But my doubt is how will you distinguish between detached and persistent object because both will have non-null id. Persistent object is attached to session and detached object is not attached to session. But how will I know if the object is attached or not attached to session.

Further to that if I have legacy database with composite id. For example following mapping of Doc class.


<class name="com.test.web.Doc">
<composite-id >
<key-property name="docId" column="docId" type="integer"></key-property>
<key-property name="docDate" column="docDate" type="timestamp"></key-property>
<key-many-to-one name="cust" class="com.test.web.Cust" column="custId"></key-many-to-one>
</composite-id>
</class>


Here id is not generated by hibernate. Suppose I assign values to id when object is new (transient), how will I know by code if this object is transient or persistent because the id is not null.


Please correct me if I am wrong in my understanding somewhere.


Regards

Sagar
[ December 02, 2008: Message edited by: Sagar Kale ]
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sagar,

I recommend you to look for the interface org.hibernate.Session there you can find the method contains, this method will look if the given object it's associated with that session (persistent), if not, doesn't mean that it's detached, because it could be associated with other session if any.
[ December 02, 2008: Message edited by: Angel Taveras ]
 
Sagar Kale
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angel,

Thank you very much.

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic