Stupid nHibernate.
So, after spending ages trying to get something as simple as a rowcount, today I have spent an equally frustrating period trying to get a results set which does a logic OR operation across a joined table. Something as petty as this, in SQL terms:
SELECT * from product p
inner join sku s on s.id = p.id
WHERE p.Name like '%widget%'
OR s.SkuCode like '%wd_%'
The issue arises because even though my mappings are correct, nHibernate throws a wobbler at you if you start with Product and try and get it to recognise that it should be able to filter on a field in the joined sku table. This, for example, compiles but fails horribly even though to my mind it's the intuitive way of presenting the above query in nHibernate:
Dim results As ArrayList = session.CreateCriteria(Of DataTransferObjects.Product) _
.Add(Expression.Or( _
Expression.Like("Name", "widget"), _
Expression.Like("SkuCode", "wd_"))) _
.List()
No, what you have to do is set up a CreateAlias to the table and use that:
Dim results As ArrayList = session.CreateCriteria(Of DataTransferObjects.Product) _
.CreateAlias("Skus", "sku") _
.Add(Expression.Or( _
Expression.Like("Name", "widget"), _
Expression.Like("sku.SkuCode", "wd_"))) _
.List()
Certainly not the way I'd expect a "highly intuitive", productivity-enhancing framework to function. Not by a long shot.
I could have done the required work by creating a couple of custom data handling objects and accompanying T-SQL stored procedures in about ten minutes. To do the same thing in nHibernate has taken two days. What's that about enhancing productivity?