Getting NHibernate error Exception of type 'Antlr.Runtime.NoViableAltException' was thrown
Asked Answered
C

1

7

I'm having some issues with a query of a table that includes a space in it's name

If i write a sql-query it works fine, ie SELECT * FROM [product groups], but when using NHibernate CreateQuery everything breaks

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

Will generate the error

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()
...

If I use a CreateSQLQuery it works

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

The mapping file looks like this

<?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>

Why would not CreateQuery work?

Celesta answered 29/6, 2013 at 21:50 Comment(0)
B
9

The CreateQuery(), described here: 9.3.2. The IQuery interface, is the way how to query your Entities with 14. HQL: The Hibernate Query Language.

I.e. you have to use your domain model names ProductGroups instead of the table name [product groups]

//IQuery query = session.CreateQuery("FROM [product groups]");
IQuery query = session.CreateQuery("FROM ListModels.ProductGroups as pg");
Benjie answered 30/6, 2013 at 7:30 Comment(1)
This is perfectly correct, I also had to set the table parameter in the class in the hbm file, thanks.Celesta

© 2022 - 2024 — McMap. All rights reserved.