开发者

Why doesn't ICriteria pass variable value to the withClause?

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>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜