Hi,
Thanks for this answer but the question is for an HQL query, not an SQL.
In fact, there is probably a bug because
- writing the " AS " keyword in the SELECT clause of an HQL causes an exception like ", expected in SELECT"
- not giving any alias in the SELECT of an HQL causes nhibernate to send a null argument to the method TransformTuple of
AliasToBeanResultTransformer and that's why you have the null reference exception.
As a quick work around, i created my own ResultTransfomer wich works without alias in the SELECT. Here is the code :
Code:
public class PropertyInjecterResultTransformer : IResultTransformer
{
private Type resultType;
private List<string> properties = null;
public PropertyInjecterResultTransformer(Type type, ICollection<string> properties)
: base()
{
resultType = type;
this.properties = CollectionUtil.ToList(properties);
}
//the order of the field in your HQL query SELECT must be the same as the order of the properties given to me
public object TransformTuple(object[] tuple, string[] aliases)
{
object result = Activator.CreateInstance(resultType);
for (int i = 0; i < properties.Count; i++)
{
ReflectionUtil.Set(result, properties[i], tuple[i]);
}
return result;
}
public IList TransformList(IList collection)
{
return collection;
}
}
To use this transformer, you need to give the property list like this :
Code:
query.SetResultTransformer(new PropertyInjecterResultTransformer(
typeof(CustomerDTO),
new string[] {
"Name",
"CityName",
"TotalPurchases"
}));
I hope this will help.
Cya.