title: NHibernate LINQ Query - object references an unsaved transient instance - Without saving
body: <p>I've got a weird exception when I execute an nHibernate LINQ query. 
  If I write 
  <br/>
  <code>
  var address = new Address {Id = Guid.Empty};
  session.Query().Where(x=> x == address).ToList();
  </code>
  <br/></p>
  
  <p>Than I get</p>
  
  <blockquote>
    <p>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</p>
    
    <p>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,
    IList<code>1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset,
    IList</code>1 sqlQueryParametersList, QueryParameters queryParameters,
    ISessionImplementor session)    at
    NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand command,
    IList<code>1 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, ISet</code>1 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)<br>
    at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression
    expression)    at Remotion.Linq.QueryableBase<code>1.GetEnumerator()    at
    System.Linq.Buffer</code>1..ctor(IEnumerable<code>1 source)    at
    System.Linq.Enumerable.ToArray[TSource](IEnumerable</code>1 source)</p>
  </blockquote>
  
  <p>However, if I Write
  <br/>
  <code>
  var person = new Person {Id = Guid.Empty};
  session.Query().Where(x=> x == person).ToList();
  </code>
  <br/></p>
  
  <p>everything works fine. The only difference between the two classes and related mapping is that Address is mapped using the &lt; class /> tag while Person belongs to an inheritance mapping, Party => Person and is mapped using &lt; joined-subclass /></p>
  
  <p>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.</p>
  
  <p>Any idea on this behavior ?</p>
  
  <p>Thanks,
  Marco</p>