<p>I've come back to using NHibernate after using other technologies (<a href="http://www.lhotka.net/" rel="nofollow noreferrer">CSLA</a> and <a href="http://subsonicproject.com/" rel="nofollow noreferrer">Subsonic</a>) for a couple of years, and I'm finding the querying a bit frustrating, especially when compared to Subsonic. I was wondering what other approaches people are using?</p>

<p>The Hibernate Query Language doesn't feel right to me, seems too much like writing SQL, which to my mind is one of the reason to use an ORM tools so I don't have to, furthermore it's all in XML, which means it's poor for refactoring, and errors will only be discovered at runtime?</p>

<p>Criteria Queries, don't seem fluid enough.</p>

<p>I've <a href="http://jhollingworth.wordpress.com/2008/03/28/subsonic-like-nhibernate-query-generator-button-in-visual-studio/" rel="nofollow noreferrer">read</a> that Ayende's  <a href="http://www.ayende.com/projects/downloads/nhibernate-query-generator.aspx" rel="nofollow noreferrer">NHibernate Query Generator</a>, is a useful tool, is this what people are using? What else is out there?</p>

<p>EDIT: Worth a read
<a href="http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx" rel="nofollow noreferrer">http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx</a></p>

[{'answer_id': 43409, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': '<p>The thing with LINQ for NHibernate is still in beta; I\'m looking forward to NHibernate 2.1, where they say it will finally make the cut.</p>\n\n<p>I made a presentation on LINQ for NHibernate around a month ago, you might find it useful. I blogged about it here, including slides and code:</p>\n\n<p><a href="http://dotnet.kapenilattex.com/?p=85" rel="nofollow noreferrer">LINQ for NHibernate: O/R Mapping in Visual Studio 2008 Slides and Code</a></p>\n'}, {'answer_id': 48097, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 2, 'selected': False, 'text': '<p>To rid yourself of the XML, try <a href="http://code.google.com/p/fluent-nhibernate/" rel="nofollow noreferrer">Fluent NHibernate</a></p>\n\n<p>Linq2NH isn\'t fully baked yet.  The core team is working on a different implementation than the one in NH Contrib. It works fine for simple queries though.  Use sparingly if at all for best results.</p>\n\n<p>As for how to query (hql vs. Criteria vs. Linq2NH), expose intention-revealing methods (<code>GetProductsForOrder(Order order)</code>, <code>GetCustomersThatPurchasedProduct(Product product)</code>, etc) on your repository interface and implement them in the best way.  Simple queries may be easier with hql, while using the specification pattern you may find the Criteria API to be a better fit.  That stuff just stays encapsulated in your repository, and if your tests pass it doesn\'t much matter how you implement.</p>\n\n<p>I\'ve found that the Criteria API is cumbersome and limiting but flexible.  HQL is more my style (and it\'s better than SQL - it\'s object based, not schema based) and seems to work better for me for simple GetX methods..</p>\n'}, {'answer_id': 206242, 'author': 'tobinharris', 'author_id': 1136215, 'author_profile': 'https://Stackoverflow.com/users/1136215', 'pm_score': 1, 'selected': False, 'text': '<p>I use Linq for NHibernate by default. When I hit bugs or limitations, I switch to HQL. </p>\n\n<p>It\'s a clean approach if you keep all your queries together in a data access class, such as a Repository. </p>\n\n<pre><code>public class CustomerRepostitory()\n{ \n  //LINQ for NHibernate     \n  public Customer[] FindCustomerByEmail(string email)\n  {\n     return (from c in _session.Linq&lt;Customer&gt;() where c.Email == email).FirstOrDefault();\n  }\n\n  //HQL\n  public Customer[] FindBestBuyers()\n  {\n    var q = _session.CreateQuery("...insert complex HQL here...");\n    return q.List&lt;Customer&gt;();\n  }\n}\n</code></pre>\n\n<p>You asked about refactoring. LINQ is obviously taken care of by the IDE, so for any remaining HQL, it\'s fairly easy to scan these repository classes and change HQL by hand.</p>\n\n<p>Putting HQL in XML files is a good practice, maybe see if the <a href="http://www.lieser-online.de/blog/?p=84" rel="nofollow noreferrer">ReSharper NHIbernate plugin</a> can handle the query refactoring by now?</p>\n\n<p>A <strong>big</strong> improvement when writing or refactoring queries (HQL or LINQ) is to put finder methods under <strong>unit test</strong>. This way you can quickly tweak the HQL/LINQ until you get a green bar. The compile/test/feedback loop is very fast, especially if you use an in-memory database for testing.  </p>\n\n<p>Also, if you <em>forget</em> to edit the HQL after refactoring, the unit tests should let you know about your broken HQL very quickly.</p>\n'}, {'answer_id': 332588, 'author': 'Mauricio Scheffer', 'author_id': 21239, 'author_profile': 'https://Stackoverflow.com/users/21239', 'pm_score': 1, 'selected': False, 'text': '<p>An alternative to LINQ-to-NHibernate and Ayende\'s NHQG is to generate NHibernate Expressions/Restrictions from C#3 Expressions. This way you get a more strongly-typed Criteria API.</p>\n\n<p>See:</p>\n\n<ul>\n<li><a href="http://bugsquash.blogspot.com/2008/03/strongly-typed-nhibernate-criteria-with.html" rel="nofollow noreferrer">http://bugsquash.blogspot.com/2008/03/strongly-typed-nhibernate-criteria-with.html</a></li>\n<li><a href="http://www.kowitz.net/archive/2008/08/17/what-would-nhibernate-icriteria-look-like-in-.net-3.5.aspx" rel="nofollow noreferrer">http://www.kowitz.net/archive/2008/08/17/what-would-nhibernate-icriteria-look-like-in-.net-3.5.aspx</a></li>\n<li><a href="http://nhibernate.info/blog/2009/01/07/typesafe-icriteria-using-lambda-expressions.html" rel="nofollow noreferrer">http://nhibernate.info/blog/2009/01/07/typesafe-icriteria-using-lambda-expressions.html</a></li>\n</ul>\n'}, {'answer_id': 512997, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>scrap nHibernate and go back to Subsonic if you can.  In my opinion, Subsonic is a far more fluent and testable ORM/DAL.  I absolutely hate HQL what\'s the point of a weakly typed query in an ORM?  And why would I use Linq/nH/SQL when I can just use Linq to SQL and cut out a layer?</p>\n\n<p>nHibernate was a good ORM when Subsonic wasn\'t around, but now, it\'s just plain awful to work with in comparison.  It easily takes me 2 times longer to do stuff with nHibernate vs Subsonic.  Testing is a pain since nHibernate is runtime, so now I need to employ a few QA  engineers to "click" around the site instead of getting a compile time error.</p>\n'}]
['https://Stackoverflow.com/questions/43393', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/230/']