Description
HQL and LINQ query by the type on <any/>
with meta-type="string"
fails with System.Data.SqlClient.SqlException : Invalid column name
. Looks like the type makes into SQL query text without single quotes.
To replicate the issue, follow the steps:
- Map using
<any/>
withmeta-type="string"
:
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" />
<any name="Shape" id-type="int" meta-type="string">
<meta-value value="CircleType" class="Circle"/>
<meta-value value="SquareType" class="Square"/>
<column name="ShapeType" />
<column name="ShapeId" />
</any>
- Try to query by type using HQL:
var boxes = s.CreateQuery("from ToyBox t where t.Shape.class = Square").List<ToyBox>();
or LINQ:
var boxes =
(from t in s.Query<ToyBox>()
where t.Shape is Square
select t).ToList();
- The Query will fail witch exception:
NHibernate.Exceptions.GenericADOException : could not execute query
[ select toybox0_.Id as id1_0_, toybox0_.Name as name2_0_, toybox0_.ShapeType as shapetype3_0_, toybox0_.ShapeId as shapeid4_0_ from ToyBox toybox0_ where toybox0_.ShapeType=SquareType ]
[SQL: select toybox0_.Id as id1_0_, toybox0_.Name as name2_0_, toybox0_.ShapeType as shapetype3_0_, toybox0_.ShapeId as shapeid4_0_ from ToyBox toybox0_ where toybox0_.ShapeType=SquareType]
----> System.Data.SqlClient.SqlException : Invalid column name 'SquareType'.
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces)
at NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results, Object filterConnection)
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
at NHibernate.Impl.AbstractSessionImpl.List[T](IQueryExpression query, QueryParameters parameters)
at NHibernate.Impl.AbstractQueryImpl2.ListT
at NHibernate.Test.NHSpecificTest.GHHQL.Fixture.AnyIs_HqlWithClassNameInTheRight()
There are related issues fixed in a past: NH-2741, NH-2328, NH-3251. The issue NH-2328 has an unit test (NHibernate.Test.NHSpecificTest.NH2328) that demonstrates query by the type on <any/>
with meta-type "int". The unit test successfully passes. However if we change mapping from meta-type "int" to meta-type "string", the test will fail.