I have a weird problem with NHibernate. I'm requesting a simple entity object and NHibernate is always executing two identical SQL queries for that HQL query. So two SQL queries are printed to the debug window (show_sql=true) and SQL Profiler also shows two identical calls to sp_executesql.
It happens exactly at the line where the HQL is executed (with UniqueResult()). If I step into this line while debugging, I can see the following sequence:
1. First query is executed.
2. ID property is set (other properties are this new automatic properties, so I can't step into their setters).
3. Second query is executed.
All that happens before I request a child entity of that object.
I only have that problem with this query, a simple Get(id) works normally and queries to other tables also work normally.
If it helps: A unique contraint is added to the column "username".
Hibernate version:
1.2.1.GA
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Name.Library" namespace="Name.Library.BOs">
<class name="User" table="[User]">
<id name="ID">
<generator class="identity"/>
</id>
<many-to-one name="MotherLicense" column="MotherLicenseID"/>
<many-to-one name="Language" column="LanguageID"/>
<property name="Username"/>
<property name="Password"/>
[snip]
<set name="LoginLogs" inverse="true">
<key column="UserID"/>
<one-to-many class="LoginLog"/>
</set>
<set name="TemplateGroups" table="User_TemplateGroup">
<key column="UserID"/>
<many-to-many class="TemplateGroup" column="TemplateGroupID"/>
</set>
<set name="Documents" inverse="true">
<key column="UserID"/>
<one-to-many class="Document"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
return NHibernateHelper.CurrentSession.CreateQuery("from User u where u.Username = :username").SetString("username", username).UniqueResult<User>();
Full stack trace of any exception that occurs:-
Name and version of the database you are using:Microsoft SQL Server 2005
The generated SQL (show_sql=true):Code:
select user0_.ID as ID1_, user0_.MotherLicenseID as MotherLi2_1_, user0_.LanguageID as LanguageID1_, user0_.Username as Username1_, user0_.Password as Password1_, [snip]
from Name.dbo.[User] user0_
where (user0_.Username=@p0 ); @p0 = 'user'
Debug level Hibernate log excerpt:
Please inform me in which rows you are interested in. Because that simple query creates
67 lines of debug output!