2
votes

I've got a weird exception when I execute an nHibernate LINQ query. If I write
var address = new Address {Id = Guid.Empty}; session.Query().Where(x=> x == address).ToList();

Than I get

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: DomainModel.Address, Entity: DomainModel.Address

at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session) at NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand command, IList1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session) at NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand command, IList1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session) at NHibernate.SqlCommand.SqlCommandImpl.Bind(IDbCommand command, ISessionImplementor session) at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet1 querySpaces, IType[] resultTypes) at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) at NHibernate.Impl.ExpressionQueryImpl.List() at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase1.GetEnumerator() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)

However, if I Write
var person = new Person {Id = Guid.Empty}; session.Query().Where(x=> x == person).ToList();

everything works fine. The only difference between the two classes and related mapping is that Address is mapped using the < class /> tag while Person belongs to an inheritance mapping, Party => Person and is mapped using < joined-subclass />

I know that I can rewrite the query using the Key fields rather than the object, however this is only a really simplified version of the problem and this solution is not applicable.

Any idea on this behavior ?

Thanks, Marco

1

1 Answers