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

c#, nhibernate, nhibernate-mapping

Solution

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");

Problem

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?

Original source