Well, OK I exaggerate slightly, it wasn't NHibernate that wasted most of my day, but the fact that we are having to use a slightly out of date version of 2.0 to allow us to use NHSearch.
NHibernate Search is a great piece of code, it abstracts Lucene.NET away from your application, and lets you pretty much pretend that you are dealing with NHibernate. However NHSearch has slipped a little in development behind the main NHibernate trunk.
So earlier today, one of our devs got a problem with his seemingly correct query:
var newsGroupTitles = new[] { "Steel News" };
var tagTitles = new[] { "Asia" };
var criteria = DetachedCriteria.For(typeof(Article), "article")
.CreateCriteria("article.NewsGroupList", "newsGroups")
.Add(Property.ForName("newsGroups.Title").In(newsGroupTitles))
.CreateCriteria("article.TagList", "tags")
.Add(Property.ForName("tags.Title").In(tagTitles))
.AddOrder(new Order("article.Date", true));
Well, actually that query worked just fine, until you added a simple .SetMaxResults() to it ... at which point it threw an exception and told us that the id had been duplicated in the query:
System.Data.SqlClient.SqlException : The column 'articleid' was
specified multiple times for 'query'.
The column 'articleid' was specified multiple times for 'page'.
Of course this didn't look right. So I dropped it onto the NH users group on Google, and awaited someone far more knowledgeable to point out what I had done wrong. Tuna Toksöz promptly pointed out, somewhat less than helpfully, that this was probably fixed in the last two days. Of course Tuna thought he was being very helpful - unfortunately we were running a few versions behind the trunk due to NHSearch - so I couldn't get the latest version to see this bug disappear ... my only option was very loud cursing.
So with some to and fro, and some suggestions from Ayende and Fabio, I stumbled along trying to get something to work - and as per the last time I tried, I failed miserably to get all the NH components to compile against each other, NHSearch was still the limiting factor.
Eventually Tuna provided me with the HQL version of the query we were trying to do, which alleviated my initial problem, though in a less than perfect way. But, solve the problem it did, and so it will get into our code base on Monday, and probably be semi-replicated across other entities we need to query in similar ways. BIG thanks go to Tuna!
IList result = sess.CreateQuery(
"from Article a join a.TagList tag
join a.NewsGroupList newsGroup
where tag.Title in (:tagtitle)
or newsGroup.Title in (:grouptitle)
order by a.Date")
.SetParameterList("tagtitle", tagTitles)
.SetParameterList("grouptitle", newsGroupTitles)
.SetMaxResults(15).List();
An even better result, it turns out that Ayende has NHSearch on his radar imminently (this weekend was mentioned), so I am really hopeful that I can get an up to date version of all my favourite components working in harmony again, I may even be able to put Rhino Commons back in and remove my awful home grown versions of Repository and UoW!
This has to be the biggest reason to go with a mature open-source project like NHibernate, apart from it being pretty much the market leader in it's field, it has possibly the best support network you could hope for.
For all those companies that worry that using an open-source component in their code will be risky due to lack of a "real company" backing it up, I can only say:
"that big company could never be half as effective or responsive as the community support that exists around projects like NHibernate, Castle, Moq, Rhino, xUnit, and all the other great open-source work on which I depend to help me deliver high quality software"
Posted
10-17-2008 10:11 PM
by
Jak Charlton