gpt4 book ai didi

c# - 获取 NHibernate 错误 Exception of type 'Antlr.Runtime.NoViableAltException' was throwed

转载 作者:太空狗 更新时间:2023-10-29 21:28:14 26 4
gpt4 key购买 nike

我在查询名称中包含空格的表时遇到一些问题

如果我编写一个 sql 查询,它工作正常,即 SELECT * FROM [product groups],但是当使用 NHibernate CreateQuery 时,一切都会中断

using (ISession session = SessionFactory.OpenSession())
{
IQuery query = session.CreateQuery("FROM [product groups]");
IList<ProductGroups> results = query.List<ProductGroups>();
}

会产生错误

Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 5 at

NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException() at NHibernate.Hql.Ast.ANTLR.HqlParseEngine.Parse()
...

如果我使用 CreateSQLQuery 就可以了

ISQLQuery query = session.CreateSQLQuery("SELECT ID, Title, [Available as develop] FROM [product groups]").AddEntity(typeof(ProductGroups));
IList<ProductGroups> results = query.List<ProductGroups>();

映射文件是这样的

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="ListModels.ProductGroups, ListModels" lazy="true" table="Product groups">
<id name="ID">
<generator class="native" />
</id>
<property name="Title" />
<property name="AvailableAsDevelopmentLicense" column="Available as develop" />
</class>
</hibernate-mapping>

为什么 CreateQuery 不起作用?

最佳答案

此处描述的 CreateQuery():9.3.2. The IQuery interface , 是如何使用 14. HQL: The Hibernate Query Language 查询您的实体的方式.

即您必须使用您的 模型名称ProductGroups 而不是 名称[产品组]

//IQuery query = session.CreateQuery("FROM [product groups]");
IQuery query = session.CreateQuery("FROM ListModels.ProductGroups as pg");

关于c# - 获取 NHibernate 错误 Exception of type 'Antlr.Runtime.NoViableAltException' was throwed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com