org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:
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
<class name=”com.xxx.A” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
</id>…………………..
some more mapping elements
…………………..
…………………..
<many-to-one name=”bId” class=”com.xxx.B” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />
</many-to-one>
…………………..
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.
6 Comments »
Leave a comment
-
Archives
- October 2009 (2)
- September 2009 (3)
- August 2009 (2)
- July 2009 (1)
- June 2009 (2)
- May 2009 (3)
- April 2009 (8)
- February 2009 (2)
- December 2008 (1)
- November 2008 (3)
- October 2008 (1)
- January 2008 (8)
-
Categories
-
RSS
Entries RSS
Comments RSS

Hi man you made my day ….
Thanks
Excellent post. Keep up the good work.
Hi,
Thank you very much, you solved my problem
Have a nice day,
Khalid
Thanks for sharing, one quick clarification. I was not able to insert the data for mapped columns when flags are set to false. But when I set flags to true, everything works well.
Hi Sathish,
Yes when you set the flag to true Hibernate will do the inserts for those mapped columns. setting fields to false tells the hibernate that this fields are read only and hibernate should not update them.
Thanks..