NHibernate AliasToBean transformer associations

There is my master piece… which I’m using to transform any level of projections depth. Take it and use it like this: .SetResultTransformer(new DeepTransformer<MyEntity>()) It could be used for any ValueType properties, many-to-one references and also for dynamic objects… public class DeepTransformer<TEntity> : IResultTransformer where TEntity : class { // rows iterator public object TransformTuple(object[] … Read more

What are the First and Second Level caches in (N)Hibernate?

1.1) First-level cache First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every … Read more

NHibernate ISession Flush: Where and when to use it, and why?

Briefly: Always use transactions Don’t use Close(), instead wrap your calls on an ISession inside a using statement or manage the lifecycle of your ISession somewhere else. From the documentation: From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection’s state with the state of objects held in … Read more

Generic Repository or Specific Repository for each entity?

To begin with, if you are using full ORM like Entity Framework or NHibernate, you should avoid implementing additional layer of Repository and Unit Of Work. This is because; the ORM itself exposes both Generic Repository and Unit Of Work. In case of EF, your DbContext is Unit Of Work and DbSet is Generic Repository. … Read more

Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

I just come up to that question, and realized, that there is missing any answer. And it is a shame, while I do often point out this NHibernate documentation statement: 24. Best Practices Don’t use exotic association mappings. Good usecases for a real many-to-many associations are rare. Most of the time you need additional information … Read more

Query on HasMany reference

As almost always, NHibernate does have answer for this. What we are here trying to achieve would be a SQL Statement lookin like this: // final Request selection SELECT request.[RequestId] FROM [Request] request // Only requests, which are successful, and have Max(date) WHERE request.[RequestId] IN ( SELECT successResponse.RequestId as y0_ FROM [Response] successResponse // response … Read more

How to Eager Load Associations without duplication in NHibernate?

Fetching Collections is a difficult operation. It has many side effects (as you realized, when there are fetched more collections). But even with fetching one collection, we are loading many duplicated rows. In general, for collections loading, I would suggest to use the batch processing. This will execute more SQL queries… but not so much, … Read more