NHibernate常见错误

  1. NHibernateSample.Data.Test.QueryHQLFixture.WhereTest:

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : 引发类型为“Antlr.Runtime.NoViableAltException”的异常。 near line 1, column 7 [select from   NHibernateSample.Domain.Entities.Customer c where c.Firstname='scenery']

错误语法: select  from customer

正确语法: from customer

  1. NHibernateSample.Data.Test.QueryOrderFixture.UseSQL_GetCustomersWithOrdersTest:

NHibernate.Exceptions.GenericADOException : could not execute query

[ select distinct customer.* from Customer customer inner join [Order] o on o.CustomerId=customer.CustomerId where o.OrderDate> @p0 ]

  Name:orderDate - Value:2011-9-5 0:00:00

[SQL: select distinct customer.* from Customer customer inner join [Order] o on o.CustomerId=customer.CustomerId where o.OrderDate> @p0]

  ----> System.IndexOutOfRangeException : CustomerId0_0_

错误写法:return _session.CreateSQLQuery("select distinct {customer}.* from Customer {customer}" +

            " inner join [Order] o on o.CustomerId={customer}.CustomerId where o.OrderDate> :orderDate")

                .AddEntity("customer", typeof(Customer))

                .SetDateTime("orderDate", orderDate)

                .List<Customer>();

正解写法:return _session.CreateSQLQuery("select distinct {customer.*} from Customer {customer}" +

            " inner join [Order] o on o.CustomerId={customer}.CustomerId where o.OrderDate> :orderDate")

                .AddEntity("customer", typeof(Customer))

                .SetDateTime("orderDate", orderDate)

                .List<Customer>();

  1. NHibernateSample.Data.Test.ManyToManyFixture.UseCriteriaAPI_GetCustomerswithOrdersHavingProductTest: NHibernate.Exceptions.GenericADOException : could not execute query

[ SELECT this_.CustomerId as CustomerId0_2_, this_.Version as Version0_2_, this_.Firstname as Firstname0_2_, this_.Lastname as Lastname0_2_, order1_.OrderId as OrderId1_0_, order1_.OrderDate as OrderDate1_0_, order1_.CustomerId as CustomerId1_0_, products5_.[Order] as Order1_, product2_.ProductId as Product, product2_.ProductId as ProductId3_1_, product2_.Name as Name3_1_, product2_.Cost as Cost3_1_ FROM Customer this_ inner join [Order] order1_ on this_.CustomerId=order1_.CustomerId inner join OrderProduct products5_ on order1_.OrderId=products5_.[Order] inner join Product product2_ on products5_.Product=product2_.ProductId WHERE this_.Firstname = @p0 and order1_.OrderDate > @p1 and product2_.Name = @p2 ]

Positional parameters:  #0>scenery #1>2008-10-1 0:00:00 #2>shirt

[SQL: SELECT this_.CustomerId as CustomerId0_2_, this_.Version as Version0_2_, this_.Firstname as Firstname0_2_, this_.Lastname as Lastname0_2_, order1_.OrderId as OrderId1_0_, order1_.OrderDate as OrderDate1_0_, order1_.CustomerId as CustomerId1_0_, products5_.[Order] as Order1_, product2_.ProductId as Product, product2_.ProductId as ProductId3_1_, product2_.Name as Name3_1_, product2_.Cost as Cost3_1_ FROM Customer this_ inner join [Order] order1_ on this_.CustomerId=order1_.CustomerId inner join OrderProduct products5_ on order1_.OrderId=products5_.[Order] inner join Product product2_ on products5_.Product=product2_.ProductId WHERE this_.Firstname = @p0 and order1_.OrderDate > @p1 and product2_.Name = @p2]

  ----> System.Data.SqlClient.SqlException : 列名 'Order' 无效。

列名 'Product' 无效。

列名 'Order' 无效。

Map中 column设计错误

  1. NHibernateSample.Data.Test.ManyToManyFixture.UseHQL_GetCustomersWithOrdersHavingProductTest:

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : c.Orders.elements is not mapped [select distinct c from Customer c , c.Orders.elements o where o.OrderDate > :orderDate]

错误语法:

select distinct c from Customer c ,c.Orders o where o.OrderDate > :orderDate

正确语法:

select distinct c from Customer c inner join c.Orders o where o.OrderDate > :orderDate

  1. NHibernateSample.Data.Test.ManageCustomerTransactionsFixture.DeleteCustomerTest:NHibernate.Exceptions.GenericADOException : could not delete: [NHibernateSample.Domain.Entities.Customer#5][SQL: exec CustomerDelete ? ?]  ----> System.Data.SqlClient.SqlException : '@p1' 附近有语法错误。
  1. NHibernateSample.Data.Test.ManageCustomerTransactionsFixture (TestFixtureSetUp):NHibernate.MappingException : NHibernateSample.Domain.Mappings.Customer.hbm.xml(29,6): XML validation error: 元素 命名空间“urn:nhibernate-mapping-2.2”中的“class”。 的子元素 命名空间“urn:nhibernate-mapping-2.2”中的“sql-insert”。 无效。应为可能元素的列表: 命名空间“urn:nhibernate-mapping-2.2”中的“filter, resultset, query, sql-query”。。

  ----> System.Xml.Schema.XmlSchemaValidationException : 元素 命名空间“urn:nhibernate-mapping-2.2”中的“class”。 的子元素 命名空间“urn:nhibernate-mapping-2.2”中的“sql-insert”。 无效。应为可能元素的列表: 命名空间“urn:nhibernate-mapping-2.2”中的“filter, resultset, query, sql-query”。。

原因:增删改顺序错误

正确顺序:增修改查

  1. NHibernateSample.Data.Test.ManageCustomerTransactionsFixture.CreateCustomerTest:NHibernate.Exceptions.GenericADOException : could not execute batch command.[SQL: SQL not available]

  ----> System.Data.SqlClient.SqlException : 不能将值 NULL 插入列 'CustomerId',表 'NHibernateSample.dbo.Customer';列不允许有空值。INSERT 失败。

语句已终止。

原因:用自动生成存储过程的代码顺序错误需要调整代码,查看没有用存储过程的生成的SQL语句

NHibernateSample.Data.Test.ManageCustomerTransactionsFixture.UpdateCustomerTest:

NHibernate.Exceptions.GenericADOException : could not update: [NHibernateSample.Domain.Entities.Customer#6][SQL: exec CustomerUpdate ?,?,?,?,?]

  ----> System.Data.SqlClient.SqlException : 为过程或函数 CustomerUpdate 指定了过多的参数。

原因:传的参数过多。

NHibernateSample.Data.Test.ManageCustomerTransactionsFixture.UpdateCustomerTest:

NHibernate.Exceptions.GenericADOException : could not update: [NHibernateSample.Domain.Entities.Customer#6][SQL: exec CustomerUpdate ?,?,?,?]

  ----> System.Data.SqlClient.SqlException : 从数据类型 nvarchar 转换为 int 时出错。

原因:用自动生成存储过程的代码顺序错误需要调整代码,查看没有用存储过程的生成的SQL语句

 

  1. NHibernateSample.Data.Test.ManageCustomerTransactionsFixture.ScalarStoredProcedureTest:NHibernate.MappingException : Named query not known: ScalarSProcs

原因:<spl-query>放在class里,NHibernate找不到,放在class下面即可。

  1. NHibernateSample.Data.Test.StoredProcedureTest (TestFixtureSetUp):

NHibernate.MappingException : unknown class DomainModel.Entities.Customer, DomainModel

  ----> System.IO.FileNotFoundException : 未能加载文件或程序集“DomainModel”或它的某一个依赖项。系统找不到指定的文件。

  1. NHibernateSample.Data.Test.SchemaUpdateFixture.UpdateExistingDatabaseSchemaTest:NHibernate.MappingException : Could not compile the mapping document: (string)

  ----> NHibernate.DuplicateMappingException : Duplicate class/entity mapping NHibernateSample.Domain.Entities.Product

原因:Product.hbm.xml映射文件已经存在,需要将其删除后,才能调用。

  1. NHibernateSample.Data.Test.SchemaUpdateFixture.CreateProductSchemaTest:NHibernate.MappingException : No persister for: NHibernateSample.Domain.Entities.CategorySchema

三种原因:

第一hibernate.cfg.xml配置文件错误:<mapping assembly="NHibernateSample.Domain"/>

第二ProductSchema.hbm.xml映射文件生成操作应为:嵌入的资源

第三ProductSchema.hbm.xml映射文件命名错了。少了.hbm.

原文地址:https://www.cnblogs.com/nyzhai/p/3010655.html