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.  [ 2 posts ] 
Author Message
 Post subject: Why does NHibernate create the following SQL?
PostPosted: Wed Apr 15, 2009 11:19 am 
Newbie

Joined: Wed Apr 15, 2009 11:09 am
Posts: 1
Like the title says, why does the following call in code, create the below SQL?


CODE:
Code:
Session.Linq<Shipment>().Where(x => x.Id == id).FirstOrDefault();


SQL:
Code:
SELECT   TOP 1 Id17_0_,
               Reference17_0_,
               Contract3_17_0_,
               SAPNumber17_0_,
               GoodsQua5_17_0_,
               GoodsDes6_17_0_,
               Coordina7_17_0_,
               Client8_17_0_,
               formula2_0_
FROM   
         (
            SELECT ROW_NUMBER ( ) OVER ( ORDER BY __hibernate_sort_expr_0__ ) as
                     row,
                   query.Id17_0_,
                   query.Reference17_0_,
                   query.Contract3_17_0_,
                   query.SAPNumber17_0_,
                   query.GoodsQua5_17_0_,
                   query.GoodsDes6_17_0_,
                   query.Coordina7_17_0_,
                   query.Client8_17_0_,
                   query.formula2_0_,
                   query.__hibernate_sort_expr_0__
            FROM 
                   (
                      SELECT this_.Id                  as Id17_0_,
                             this_.Reference           as Reference17_0_,
                             this_.ContractType        as Contract3_17_0_,
                             this_.SAPNumber           as SAPNumber17_0_,
                             this_.GoodsQuantity       as GoodsQua5_17_0_,
                             this_.GoodsDescription    as GoodsDes6_17_0_,
                             this_.Coordinator_User_Id as Coordina7_17_0_,
                             this_.Client_id           as Client8_17_0_,
                             (
                                select count ( * )
                                from   Comment_Contract
                                where  Comment_Contract.Contract_id = this_.Id
                             )                         as formula2_0_,
                             CURRENT_TIMESTAMP         as __hibernate_sort_expr_0__
                      FROM   [Contract] this_
                      WHERE  this_.Id = 6 /* @p0 */
                   ) query
         ) page
WHERE    page.row > 0
ORDER BY __hibernate_sort_expr_0__



I'm talking specifically about the "TOP 1" and the paging nested-selects. I'm trying to add some FetchType="Joins" to my mappings, but the TOP 1 is preventing that from working

Do I have some other code elsewhere which is likely to be causing this? If so, what is it likely to be so i can hunt it down. If not, what can i do to prevent it?

Something like this is what i'd expect (want) to be run:

Code:
SELECT this_.Id                  as Id17_0_,
                             this_.Reference           as Reference17_0_,
                             this_.ContractType        as Contract3_17_0_,
                             this_.SAPNumber           as SAPNumber17_0_,
                             this_.GoodsQuantity       as GoodsQua5_17_0_,
                             this_.GoodsDescription    as GoodsDes6_17_0_,
                             this_.Coordinator_User_Id as Coordina7_17_0_,
                             this_.Client_id           as Client8_17_0_,
                             (
                                select count ( * )
                                from   Comment_Contract
                                where  Comment_Contract.Contract_id = this_.Id
                             )                         as formula2_0_,
                             CURRENT_TIMESTAMP         as __hibernate_sort_expr_0__
                      FROM   [Contract] this_
                      WHERE  this_.Id = 6 /* @p0 */


Thanks,

Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 11:53 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not familiar with Linq to hibernate, but it's probably the FirstOrDefault() call which is tranlated to TOP(1).

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.