I'm trying to restrict a left outer join with a simple restriction but it fails with an sql exception.
System.Data.SqlClient.SqlException: Must declare the scalar variable "@p1".
My ICriteria:
var r = session.CreateCriteria<Parent>("p")
.CreateCriteria("p.Children", "c", NHibernate.SqlCommand.JoinType.LeftOuterJoin, Restrictions.Eq("c.Name", "John Doe"))
.List();
The desired SQL:
SELE开发者_开发问答CT * FROM Parents p
LEFT OUTER JOIN Children c ON c.ParentID = p.ID AND c.Name = 'John Doe'
Do I have to add the variable values in any way? The SQL generated by NHibernate is correct but the variable just isn't sent to the server.
I think that you may have found a bug in NHibernate ICriteria where you join with LeftOuterJoin on one collection but entity has two or more collections. In this case it will generate joins with 'and' on both collections with two parameters, but only supply one. And database will throw:
Insufficient parameters supplied to the command
It might be a good idea to open a bug. In the mean time, this is supported with HQL:
var query = session.CreateQuery(
"select p from Parent p left outer join p.Children as c with c.Name = :name");
query.SetString("name", "John Doe");
var parents = query.List<Parent>();
精彩评论