NHibernate LINQ Or HQL - nhibernate

I have heard about 2 way of querying using hibernate:
NHibernate.Linq and HQL
NHibernate.Linq or just HQL witch is faster?
Do any one have any other advantage?

I don't know Linq to NH that well. Technically is Linq to NH turned into HQL behind the scenes. It provides editor support (auto completion) and compile time validation (e.g. when typo) in contrast to pure HQL, which appears as normal strings to the developer tools.
Similar is QueryOver the successor of Criteria.

Related

NHibernate Query Generator with Fluent mappings

I was trying to find a good way to build dynamic queries with NHibernate and that lead me to NHibernate Query Generator which I like but we currently use Fluent NHibernate mappings and I wasn't sure if there was a way to use NHibernate Query Generator with Fluent NHibernate mappings. Any one have experience with this or know a better way to accomplish dynamic queries?
First, NHibernate already has ways to build dynamic queries (Criteria / QueryOver are specially useful for this). I googled "NHibernate Query Generator" and found a project that was abandoned more than 4 years ago.
Second, the method you use to create your mappings has nothing to do with the query APIs.

How to write Queries for retreving, Checking Existence in NHIBERNATE USING(FLUENTNHIBERNATE)

How to write Queries for retreving, Checking Existence, and all we do in SQL
using NHIBERNATE (FLUENTNHIBERNATE)
Is FluentNhibernate is used in these Queries.
And
Which one is better Fluent Nhibernate Mapping or XML Mapping
Is there any relation or should i know LINQ for learning Nhibernate?
i can see several questions:
"How to write Queries for retreving, Checking Existence": see here for the different querylanguages in NH http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html
"Is FluentNhibernate is used in these Queries": no FNH is used to configure NHibernate: alternative for hbm.xml and hibernate.cfg
"Which one is better Fluent Nhibernate Mapping or XML Mapping": in most cases FluentNHibernate is much easier to write than xml and covers 95% of options possible in xml. For beginners i would recommend FluentMappings because a lot of sensible defaults make it easier to get something to work.
"Is there any relation or should i know LINQ for learning Nhibernate?": LINQ is one of 6 ways to query in NHibernate. It is an elegant way but neither required nor the "works in all circumstances".

Is NHibernate LINQ stable and do all NHibernate bolt on projects allow it

I have been a long time user of Subsonic due to its ease of use and LINQ integration.
I now have to use something else because I need to be able to use Oracle.
I have 2 databases with the same schema therefore I want to have 1 set of POCO's and then change a connection string to switch between SQL & Oracle depending on the requirements.
Is this possible firstly, is LINQ fully functioning and stable in NHibernate and do Castle ActiveRecord and Fluent Hibernate allow the LINQ querying?
It is stable.
It is not fully functioning, and it is not planned to be fully functioning. I don't think there exists linq providers supporting 100% everything. The question should be: "Is it fully function for the queries you need to execute?" (The answer to that question would be yes in 99% of the cases)
You can find reported bugs/missing features in Jira
Fluent NHhibernate doesn't do any querying, just mapping. Castle active record doesn't query either. The linq namespace does not have a reference to active record or fluent and vise versa.
I wouldn't classify the NHibernate LINQ implementation as stable yet. The LINQ provider is still fairly young, so chance of hitting an unsupported query scneario still may be considerable in my opinion. However, other NHibernate query options are plentiful to workaround any issues the LINQ provider might throw up.

Validating HQL against the domain with Castle Active Record

We use a lot of view model builders which pass HQL strings to the ActiveRecordMediator.Execute method to populate search objects for our views.
Doing refactoring occassionally breaks these 'magic' hql strings (without us knowing)
I was wondering if anyone has tried using nhibernate named queries to validate HQL in Castle Active Record?
Is there another way rather than writing integration tests (we use LINQ to Nhibernate for basic searchs but its not quite there yet for our complex queries)
Anyone have suggestions how to validate HQL against your domain?
ActiveRecord supports named queries through the HqlNamedQuery attribute, see this article. By defining queries this way you get NHibernate's named query validation.

Best practices for querying with NHibernate

I've come back to using NHibernate after using other technologies (CSLA and Subsonic) 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?
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?
Criteria Queries, don't seem fluid enough.
I've read that Ayende's NHibernate Query Generator, is a useful tool, is this what people are using? What else is out there?
EDIT: Worth a read
http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx
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.
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:
LINQ for NHibernate: O/R Mapping in Visual Studio 2008 Slides and Code
To rid yourself of the XML, try Fluent NHibernate
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.
As for how to query (hql vs. Criteria vs. Linq2NH), expose intention-revealing methods (GetProductsForOrder(Order order), GetCustomersThatPurchasedProduct(Product product), 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.
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..
I use Linq for NHibernate by default. When I hit bugs or limitations, I switch to HQL.
It's a clean approach if you keep all your queries together in a data access class, such as a Repository.
public class CustomerRepostitory()
{
//LINQ for NHibernate
public Customer[] FindCustomerByEmail(string email)
{
return (from c in _session.Linq<Customer>() where c.Email == email).FirstOrDefault();
}
//HQL
public Customer[] FindBestBuyers()
{
var q = _session.CreateQuery("...insert complex HQL here...");
return q.List<Customer>();
}
}
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.
Putting HQL in XML files is a good practice, maybe see if the ReSharper NHIbernate plugin can handle the query refactoring by now?
A big improvement when writing or refactoring queries (HQL or LINQ) is to put finder methods under unit test. 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.
Also, if you forget to edit the HQL after refactoring, the unit tests should let you know about your broken HQL very quickly.
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.
See:
http://bugsquash.blogspot.com/2008/03/strongly-typed-nhibernate-criteria-with.html
http://www.kowitz.net/archive/2008/08/17/what-would-nhibernate-icriteria-look-like-in-.net-3.5.aspx
http://nhibernate.info/blog/2009/01/07/typesafe-icriteria-using-lambda-expressions.html
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?
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.