Get Distinct result set from NHibernate using Criteria API?

To perform a distinct query you can set the projection on the criteria to Projections.Distinct. You then include the columns that you wish to return. The result is then turned back into an strongly-typed object by setting the result transformer to AliasToBeanResultTransformer – passing in the type that the result should be transformed into. In … Read more

Can someone better explain what ‘Projections’ are in nHibernate?

Here’s a practical example. Let’s say that you have an online store and one of your domain classes is a Brand like “Samsung”. This class has a boatload of properties associated with it, perhaps an integer Identity, a Name, a free-text Description field, a reference to a Vendor object, and so on. Now let’s say … Read more

What is the difference between StatelessSession and Session in NHibernate?

Stateless session is not tracking entities that are retrieved. For example for regular ISession following code: var session = sessionFactory.OpenSession() using(var transaction = session.BeginTransaction()){ var user = session.Get<User>(1); user.Name = “changed name”; transaction.Commit(); } will result in update in DB. This tracking consumes memory and makes ISession performance to degrade over time since amount of … Read more

Difference between FluentNHibernate and NHibernate’s “Mapping by Code”

Fluent NH Fluent NHibernate offers an alternative to NHibernate’s standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code. vs. NH’s new mapping by code It is an XML-less mapping solution being an integral part of NHibernate … Read more

Install NHibernate 3.2 with NuGet

NHibernate 3.2 comes with its own proxy factory. If you’re using a config file, you just need to remove the proxyfactory configuration property. I believe the version of Fluent NHibernate that you’re using defaults to use NHibernate.ByteCode.Castle. In that case, you would need to override that setting with the built in NHibernate 3.2 proxy factory: … Read more

NHibernate Transactions on Reads

This post from one of the authors might have your answer: Even if we are only reading data, we want to use a transaction, because using a transaction ensure that we get a consistent result from the database. NHibernate assume that all access to the database is done under a transaction, and strongly discourage any … Read more

NHibernate 3.0: No FirstOrDefault() with QueryOver?

I have now found out that I could use the Take() extension method on the IQueryOver instance, and only the enumerate to a list, like so: Result precedingOrMatchingResult = Session.QueryOver<Result>(). Where(r => r.TimeStamp < timeStamp). OrderBy(r => r.TimeStamp).Desc. Take(1).List(). //enumerate only on element of the sequence! FirstOrDefault(); //get the preceding or matching result, if there … Read more