Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate object references an unsaved transient instance save the transient instance before flushing

Tags:

c#

nhibernate

I am trying to save a complex object which has many referenced elements inside and it works perfectly most of the time.
However in some cases we are getting the below exception,

object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Namespace.Core.Client.ClientDetails, Entity: Namespace.Core.Client.ClientDetails

The problem is, there are around 12 ClientDetails elements inside my complex object which we are trying to save.Is there a way to identify which object instance caused this issue? through NHibernate logging or some other way? My code sample used for save as below,

_repository.Save<SuperParent>(obj);
_repository.Flush();

Please note when i set the Nhibernate show_sql to true i am able to see all the queries properly generated, but when the flush is called, the exception is thrown.

Please help to resolve the issue.

like image 791
Naganathan Avatar asked Nov 17 '13 05:11

Naganathan


1 Answers

The exception means that there is an unsaved instance of ClientDetails referenced by this object. You have to either save it manually before saving the parent

session.Save(Parent.SomeDetail);

or set Cascade.SaveOrUpdate on the reference-mappings in the parent mapping.

like image 69
Firo Avatar answered Oct 26 '22 05:10

Firo