J'utilise NHibernate 2.0, et lorsque je soumets une requête demandant le retour des 2 premiers enregistrements, j'obtiens un certain nombre de clauses ORDER BY dans mon SQL. Lorsque je supprime les résultats Max, la requête semble correcte (aucune instruction ORDER BY). Pourquoi NHibernate l'ajoute-t-il automatiquement lorsque je cherche un sous-ensemble d'enregistrements? Merci à l'avanceNHibernate inattendu Instruction ORDER BY
Voir les instructions SQL ci-dessous:
ORDER inattendue PAR
exec sp_executesql N'SELECT TOP 2 Person1_36_0_, LastReco2_36_0_, SSN3_36_0_, FirstName4_36_0_, LastName5_36_0_, MiddleIn6_36_0_, Title7_36_0_, Suffix8_36_0_, DateOfBi9_36_0_, IsDeceased10_36_0_, Decease11_36_0_, Contact12_36_0_, MailHol13_36_0_, MailHol14_36_0_, MailHol15_36_0_, Preferr16_36_0_, CreatedBy17_36_0_, Created18_36_0_, ModifiedBy19_36_0_, Modifie20_36_0_ FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_0__) as row, query.Person1_36_0_, query.LastReco2_36_0_, query.SSN3_36_0_, query.FirstName4_36_0_, query.LastName5_36_0_, query.MiddleIn6_36_0_, query.Title7_36_0_, query.Suffix8_36_0_, query.DateOfBi9_36_0_, query.IsDeceased10_36_0_, query.Decease11_36_0_, query.Contact12_36_0_, query.MailHol13_36_0_, query.MailHol14_36_0_, query.MailHol15_36_0_, query.Preferr16_36_0_, query.CreatedBy17_36_0_, query.Created18_36_0_, query.ModifiedBy19_36_0_, query.Modifie20_36_0_, query.__hibernate_sort_expr_0__ FROM (SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_, this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_, this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased] as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as Modifie20_36_0_, CURRENT_TIMESTAMP as __hibernate_sort_expr_0__ FROM MC_Person this_ WHERE this_.[SSN] = @p0) query) page WHERE **page.row > 0 ORDER BY __hibernate_sort_expr_0__**', N'@p0 nvarchar(9)',@p0=N'123456789'
SQL correcte (sans obtenir Top 2 records)
exec sp_executesql N'SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_, this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_, this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased] as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as Modifie20_36_0_ FROM MC_Person this_ WHERE this_.[SSN] = @p0', N'@p0 nvarchar(9)',@p0=N'123456789'
J'obtiens les bons résultats. J'étais juste confus quant à pourquoi NHibernate a inséré des instructions ORDER BY quand je n'en ai jamais demandé une –