1

I'm trying to create Criteria as follow:

var criteria - session.CreateSQLQuery("select * from myTable where ID in (select id from tableB) AND ANOTHERID IN(select id from tableC)");

criteria.SetResultTransformer(Transformers.AliasToBean(typeof(myClass))).List<MyClass>().ToList()

And got the following exception:

Could not find a setter for property 'NAME' in class 'MYCLASS'

hbm property:

<property name="Name" type="string" column="NAME" not-null="true" update="true" insert="true"/>

.cs:

private string name;
public virtual string Name
{
   get {return this.name;}
   set { this.name=value;}
}

What I'm missing here?

hazzik
  • 13,019
  • 9
  • 47
  • 86
ArmoArmo
  • 105
  • 10
  • Probably an issue with an alias name in your SQL. See this [SO article](https://stackoverflow.com/questions/29805820/how-to-solve-this-exception-could-not-find-a-setter-for-property-productcode) – Hopeless May 28 '19 at 18:54

1 Answers1

2

Versions of NHibernate prior 5.0 had a bug where properties were matched case sensitively. In the 5.0+ this query would work.

AliasToBeanResultTransformer (Transformer.AliasToBean(...)) is intended to be used with "projections" and therefore does not use mappings to do the binding from columns to the class.

If you want to return an entity and want to leverage the mapping, you'll need to call AddEntity(...) on your query, like this:

var query = session.CreateSQLQuery("select * from myTable where ID in (select id from tableB) AND ANOTHERID IN(select id from tableC)");

query.AddEntity(typeof(myClass)).List<MyClass>();
hazzik
  • 13,019
  • 9
  • 47
  • 86
  • Hi, I have been tried what you suggest and got the following exception "Could not find a setter for property 'MYCLASS' in class 'MYCLASS' – ArmoArmo May 30 '19 at 15:23
  • That is really strange, because it works in my test as expected. – hazzik May 30 '19 at 22:13