website optimizer

Ads

Adbrite

Your Ad Here

Ads

Tuesday, November 25, 2008

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

I was getting this error today.
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:This is the problemTable A was referring to the talbe B using a foreign key

hbm.xml for A looked like this


some more mapping elements
…………………..
…………………..

…………………..

[

/column]


…………………..

I just want to save A with some new value and A will refer to B using some foreign key. But in my transaction I am not going to change B and save the changes. Therefore the many-to-one mapping above is wrong. It does not tell hibernate that class b is immutant. That is it is not going to change. Hibernate thinks that the referred table is not saved therefore it complains and says

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

Please note that in this case B is transient object. If you don’t really want to save the value of B(transient object) in A , then to get rid of this problem change the many-to-one mapping as

update=”false” insert=”false” fetch=”select”>

But if you want to save the value of B then see my other post

That is, tell hibernate that you are not going to change the values of B, you are just fetching it.

No comments:

AdsBy Punch

linxo