-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HQL query results in two identical SQL queries
PostPosted: Wed Aug 13, 2008 3:13 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
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!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 3:51 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Seems like the call to UniqueResult() is the reason for the two identical SQL queries. I noticed that the one call I described in my starting post was the only one to UniqueResult(). Today I added another one and that query is also executing two identical SQL queries.

Is this the expected behavior of UniqueResult()?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 5:45 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 4:17 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
Seems like an update to NHibernate 2.0.0.GA fixed this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.