繁体   English   中英

NHibernate SQL创建会导致错误

[英]NHibernate SQL creation causes error

我有2个名为Order和Orderrow的类。 我使用NHibernate来获取它的连接。

运行NUnit来测试查询时,我得到了一个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'.

在分析由NHibernate创建的SQL时,我注意到Order类正在破坏SQL语句,因为ORDER BY是SQL中的内部关键字。

这是NHibernate中创建的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

我在SQL Server 2008 Management studio中将其更改为:

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`

我在表名称Order(这样:[Order])中添加了括号,它是固定的。

但是如何在NHibernate中修复此问题呢? 是否有映射XML文件指令才能完成此操作?

(使用VS2008 SP1,SQL Server 2008 SP1,NHibernate 2.0.1 GA)

我想如果你在映射文件中放置引号(“[”和“]”在SQL Server中,或者你的数据库支持的任何引号),hibernate会在生成查询时引用对象名称。

(也许发布您的映射文件,所以我们可以看看)

请参见节“5.3。SQL引号的标识符”。 你基本上需要这个:

<class name="Order" table="`Order`">

在Fluent NHibernate中,添加

.Column("`Order`");

到您的映射来修复此错误。

使用约定时,可以添加以下内容:

public class TableNameConvention
    : IClassConvention, IClassConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
    {
        criteria.Expect(x => Check(x));
    }

    private bool Check(IClassInspector x)
    {
        return String.IsNullOrWhiteSpace(x.TableName) || x.TableName.Equals("`{0}`".Args(x.EntityType.Name));
    }

    public void Apply(IClassInstance instance)
    {
        instance.Table("`" + instance.EntityType.Name + "`");
    }
}

要解决保留表名称和保留列名称(例如'From')的问题,可以使用以下命令:

public class ColumnNameConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        instance.Column("`" + instance.Property.Name + "`");
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(c => Check(c));
    }

    private bool Check(IPropertyInspector inspector)
    {
        //walkaround:
        //this convention causes problems with Components - creates columns like Issue`Id` so we apply it only to entities

        var type = inspector.EntityType;
        if (!(type.IsSubclassOf(typeof (Entity)) || type.IsSubclassOf(typeof (GlossaryEntity))))
        {
            return false;
        }

        return true;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM