We're updating the issue view to help you get more done. 

Crash when performing a Linq query on a one-to-one mapped reference

Description

NHibernate fails to add the correct params to a SQL parameterised query when performing a Linq query upon an entity type which is mapped one-to-one. This leads to a crash from the corresponding ADO driver when NHibernate attempts to execute the query. A full reproduction solution is attached but in short, here's the problem:

1 2 3 4 5 var person = session.Load<Person>(1); var results = session.Query<Employee>() .Where(x => x.Person == person) .ToList();

That query - where the "Person" and "Employee" classes are mapped with a one-to-one relationship - crashes with the following exception:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [ERROR] FATAL UNHANDLED EXCEPTION: NHibernate.Exceptions.GenericADOException: could not execute query [ select employee0_.Id as Id1_0_, person1_.Id as Id0_1_, employee0_.StartDate as StartDate1_0_, person1_.Name as Name0_1_ from Employee employee0_ left outer join Person person1_ on employee0_.Id=person1_.Id where employee0_.Id=@p0 ] Name:p1 - Value:NHibernateBug.Person [SQL: select employee0_.Id as Id1_0_, person1_.Id as Id0_1_, employee0_.StartDate as StartDate1_0_, person1_.Name as Name0_1_ from Employee employee0_ left outer join Person person1_ on employee0_.Id=person1_.Id where employee0_.Id=@p0] ---> Mono.Data.Sqlite.SqliteException: SQLite error Insufficient parameters supplied to the command at Mono.Data.Sqlite.SqliteStatement.BindParameter (Int32 index, Mono.Data.Sqlite.SqliteParameter param) [0x00000] in <filename unknown>:0 at Mono.Data.Sqlite.SqliteStatement.BindParameters () [0x00000] in <filename unknown>:0 at Mono.Data.Sqlite.SqliteCommand.BuildNextCommand () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at NHibernate.Loader.Loader.DoList (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters, IResultTransformer forcedResultTransformer) [0x00000] in <filename unknown>:0 at NHibernate.Loader.Loader.DoList (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters) [0x00000] in <filename unknown>:0 at NHibernate.Loader.Loader.ListIgnoreQueryCache (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters) [0x00000] in <filename unknown>:0 at NHibernate.Loader.Loader.List (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters, ISet`1 querySpaces, NHibernate.Type.IType[] resultTypes) [0x00000] in <filename unknown>:0 at NHibernate.Loader.Hql.QueryLoader.List (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters) [0x00000] in <filename unknown>:0 at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List (ISessionImplementor session, NHibernate.Engine.QueryParameters queryParameters) [0x00000] in <filename unknown>:0 at NHibernate.Engine.Query.HQLQueryPlan.PerformList (NHibernate.Engine.QueryParameters queryParameters, ISessionImplementor session, IList results) [0x00000] in <filename unknown>:0 at NHibernate.Impl.SessionImpl.List (IQueryExpression queryExpression, NHibernate.Engine.QueryParameters queryParameters, IList results) [0x00000] in <filename unknown>:0

The reproduction case attached uses a SQLite database, but I have also been able to reproduce it using an MS-SQL database.

Note that I wrote the reproduction case project using Mono, and thus it depends upon Mono.Data.Sqlite at present. If you wish to execute the reproduction case on Windows/.NET, remove the reference to Mono.Data.Sqlite and download/install Sqlite from nuget.

To try the reproduction case (after making any changes for Sqlite), compile and execute the solution (there is only one project). The ReproductionCase class contains the logic which triggers this crash. The EntryPoint class just sets up and configures the session factory and db instance (including creating/exporting the schema).

Thanks

Environment

Debian Wheezy GNU/Linux with Mono:
Mono JIT compiler version 3.12.1 (tarball Fri Mar 6 19:12:47 UTC 2015)

I have also seen this reproducible on a colleagues Windows machine running the official .NET framework 4.5

Status

Assignee

Unassigned

Reporter

Craig Fowler

Components

Fix versions

Affects versions

4.0.3

Priority

Critical