I am trying to add an HqlGenerator to allow NHibernate to query a calculated property on the following domain entity:
public class User
{
public string FirstName { get; set; }
public string LastName {get; set; }
public string FullName
{
get { return FirstName + " " + LastName; }
}
}
I have created an HQL generator like so:
public class UserFullName : BaseHqlGeneratorForProperty
{
public UserFullName()
{
var properties = new List<MemberInfo> { typeof(User).GetProperty("FullName") };
SupportedProperties = properties;
}
public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var user = visitor.Visit(expression).AsExpression();
return treeBuilder.Concat(
treeBuilder.Dot(user, treeBuilder.Ident("FirstName")),
treeBuilder.Constant(" "),
treeBuilder.Dot(user, treeBuilder.Ident("LastName"))
);
}
}
The generator is wired up correctly into the configuration as the debugger will break within the BuildHql
method when I run the query:
session.Query<User>().FirstOrDefault(x => x.FullName == "Aaron Janes");
however, later on in the NHibernate internals an exception is thrown:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. [.FirstOrDefault[User](NHibernate.Linq.NhQueryable`1[User], Quote((x, ) => (String.op_Equality(x.FullName, Aaron Janes))), )]
Can anyone spot what I am doing wrong?