J'ai 2 classes nommées Order et Orderrow. J'utilise NHibernate pour obtenir une jointure.La création de NHibernate SQL provoque une erreur
Lors de l'exécution NUnit pour tester la requête, je suis un ADOException:
Logica.NHibernate.Tests.NHibernateTest.SelectAllOrdersFromSupplierNamedKnorrTest:
NHibernate.ADOException : could not execute query
[ SELECT this_.OrderId as OrderId1_1_, this_.CreatedAt as CreatedAt1_1_, this_.ShippedAt as ShippedAt1_1_, this_.ContactId as ContactId1_1_, customer2_.ContactId as ContactId0_0_, customer2_.LastName as LastName0_0_, customer2_.Initials as Initials0_0_, customer2_.Address as Address0_0_, customer2_.City as City0_0_, customer2_.Country as Country0_0_ FROM Order this_ inner join Contact customer2_ on this_.ContactId=customer2_.ContactId ]
[SQL: SELECT this_.OrderId as OrderId1_1_, this_.CreatedAt as CreatedAt1_1_, this_.ShippedAt as ShippedAt1_1_, this_.ContactId as ContactId1_1_, customer2_.ContactId as ContactId0_0_, customer2_.LastName as LastName0_0_, customer2_.Initials as Initials0_0_, customer2_.Address as Address0_0_, customer2_.City as City0_0_, customer2_.Country as Country0_0_ FROM Order this_ inner join Contact customer2_ on this_.ContactId=customer2_.ContactId]
----> System.Data.SqlClient.SqlException : Incorrect syntax near the keyword 'Order'.
Lors de l'analyse SQL qui a été créé par NHibernate, je remarque que la classe Ordre corrompt l'instruction SQL, parce que ORDER BY est un mot clé interne dans SQL.
C'est le SQL créé en NHibernate:
SELECT this_.OrderId as OrderId1_1_, this_.CreatedAt as CreatedAt1_1_, this_.ShippedAt as ShippedAt1_1_, this_.ContactId as ContactId1_1_, customer2_.ContactId as ContactId0_0_, customer2_.LastName as LastName0_0_, customer2_.Initials as Initials0_0_, customer2_.Address as Address0_0_, customer2_.City as City0_0_, customer2_.Country as Country0_0_ FROM Order this_ inner join Contact customer2_ on this_.ContactId=customer2_.ContactId
Je l'ai changeai dans SQL Server 2008 Management Studio comme ceci:
SELECT this_.OrderId as OrderId1_1_, this_.CreatedAt as CreatedAt1_1_, this_.ShippedAt as ShippedAt1_1_, this_.ContactId as ContactId1_1_, customer2_.ContactId as ContactId0_0_, customer2_.LastName as LastName0_0_, customer2_.Initials as Initials0_0_, customer2_.Address as Address0_0_, customer2_.City as City0_0_, customer2_.Country as Country0_0_ FROM [Order] this_ inner join Contact customer2_ on this_.ContactId=customer2_.ContactId`
J'ai ajouté entre parenthèses à l'ordre du nom de la table (comme celui-ci: [Ordre]) et il est fixé. Mais comment puis-je corriger cela dans NHibernate? Existe-t-il une instruction de fichier XML de mappage pour que cela soit fait?
(en utilisant VS2008 SP1, SQL Server 2008 SP1, NHibernate 2.0.1 GA)
Juste une note: Je sais que [et] ne sont pas des standards SQL, mais ce sont les citations par défaut utilisées par le serveur SQL. –
J'ai ajouté table = "[Order]" et ça marche! –
Dans NH, le backtick est effectivement utilisé pour cela, voir la réponse d'Anton. C'est la syntaxe NH indépendante de la base de données. –