Search code examples
sqlnhibernatefluent-nhibernatenhibernate-mapping

NHibernate QueryOver queries the db three times with the same query


I ran into a strange issue with NHibernate today. I am pretty sure that there's an explanation and solution for this. So here we go: I have isolated one query with the issue and the code looks as follows:

_session = _sessionFactory.OpenSession();
ITransaction _transaction = _session.BeginTransaction();

var result = _session.QueryOver<Employee>()
   .Where(x => x.uid == employeeuid)
   .SingleOrDefault();

_transaction.Commit();

The Employee object has some references etc., but I don't think this is important right now. UID is a string. What basically happens is that log4net tells me that the same query is executed three times:

SELECT <some columns...> FROM employer this_ WHERE this_.uid = <uid-string>

As you can see... a plain query. Can anyone give me a hint how to get rid of the query-overhead?

Regards, Martin


Solution

  • I would first back this up by seeing what is being queried on the database itself. To find out what the database is actualy doing either:-

    1. Run SQL profiler and profile the queries
    2. Download a trial version of NHProfiler and check on that. This is a great tool and well worth the money to purchase it if you find it helps for future issues.

    Personally I am a little suspicious of the amount of log info that log4Net spews out.