nhibernatenhibernate-mappingnhibernate-criteriahbmxml

Hibernate Criteria API with access="field" mapping


I have a problem when using Hibernate Criteria API:

var query = session.QueryOver<MyClass>().Where(param => param.Name == "myFilterName").List<MyClass>();

If a run this statement, a NHibernate.QueryException is thrown:

could not resolve property: Name of: MyClass

And in the StackTrace:

at NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName)

MyClass.hbm.xml file has the property mapped of this way:

<property name="name" access="field">
  <column name="NAME" length="50" not-null="true" />
</property>

I think that the problem comes because hibernate can not access the property "Name" of MyClass because is mapped with access="field", but I can not change this way to access the property due application design requirements. The idea is create querys by using Criteria API with lambda expressions in order to avoid hardcoded string property names.

Also I´ve tried with a Expression with the same exception result:

var criterion = Expression.Where<MyClass>(param => param.Name == "myFilterName");
var result = session.CreateCriteria<MyClass>().Add(criterion).List<MyClass>();

Somebody knows how I can indicate to Criteria API that MyClass has the properties mapped as access="field"?

Help very appreciated.


Solution

  • Not sure of what you want to achieve, having the class code might help.

    Anyway, from what you provided, I guess the mapping should be :

    <property name="Name" access="field.camelcase">

    see http://www.nhforge.org/doc/nh/en/#mapping-declaration-property