I am having trouble developing how to get the following sql in NHibernate ICriteria:
select * from Contract contract_outer where exists ( select 1 from RequiredDocRules where not exists ( select 1 from Contract contract_inner inner join ContractDocs contractDocs on cDocs.ContractId = c_inner.Id inner join Doc doc doc.Id = contractDocs.DocId where contract_inner.Id = contract_outer.Id and doc.Type = RequiredDocRules.DocType) ) and RequiredDocRules.ContractType = contract_outer.Type )
Basically, the request says: "Show me contracts that lack the required documents, based on the rules for this type of contract."
CreateCriteria is as follows:
var subqueryB = DetachedCriteria.For<Contract>("contract_inner") .CreateAlias("contract_inner.Docs", "doc") .Add(Restrictions.EqProperty("doc.Type", "rule.DocType")) .Add(Restrictions.EqProperty("contract_inner.Id", "contract_outer.Id") .SetProjection(Projections.Id()); var subqueryA = DetachedCriteria.For<RequiredDocRule>("rule") .Add(Restriction.EqProperty("rule.ContractType", "contract_outer.Type")) .Add(Subqueries.NotExists(subqueryB)) .SetProjection(Projections.Id()); return Session.CreateCriteria<Contract>("contract_outer") .Add(Subqueries.Exists(subqueryA)) .List<Contract>();
This code above throws the following unclear exception:
Could not find suitable criteria info provider to: contract_inner.Id = contract_outer.Id and doc.Type = rule.DocType
I narrowed down the problem to the limit of "contract_inner.Id = contract_outer.Id". Maybe he has problems navigating through two layers of subqueries?
I am using NHibernate 2.1 btw.
I seem to be the only person to receive this exception - if you google to report the exception, one of my Stackoverflow posts goes to number one. This question is similar, but different. So disappointing!
source share