Saturday, April 12, 2008

Track down TransientObjectException

Problem:

TransientObjectException is hibernate (nhibernate in my little world) exception which occurs when you flush.

Identify the code which causes it:

It is simple. Put nhibernate session.IsDirty() underwatch. Execute your program stpe by step. You are 'ok' till IsDirty give you 'true' or 'false'. You can identify the culprit step when IsDirty raises any exception.

Cause and Solution:

No one can you tell you exact solution as this may be caused by variety of mistakes. The root cause is that you are trying to save an object into database which has transient objects (not existing in database in our context) attached to it. Solution is to save the transient objects before flush or just remove the "cascade" attribute from the HBM.

2 comments:

Anonymous said...

how do you get ride of a transient object though? if you don't want to save it?

Yogee said...

Simply detach from the parent entity i.e. nullify parent entity's variable with which your transient object is attached.
Make sure you do not run 'flush' unless the transient object is detached.