Is it possible to add a restriction to a derived field in an entity, ie. one that is not persisted? For example, if this is my entity:
public class Employee
{
public long Id { get; set; }
public string Forename { get; set; }
public string Surname {get; set; }
public string FullName { get { return Forename + " " + Surname; }}
}
and this is the mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.Entities"
assembly="Domain">
<class name="Employee" table="`Employee`">
<id name="Id" column="Id" type="long">
<generator class="identity"/>
</id>
<property name="Forename"/>
<property name="Surname"/>
</class>
</hibernate-mapping>
And this is my query:
public Employee GetByFullName(string fullName)
{
return _session
.CreateCriteria<Employee>
.Add(Restrictions.Eq("FullName", fullName))
.List<Employee>();
}
Please ignore the fact that I could write the query myself, this is a trivial example to demonstrate. This would be useful in far more complex scenarios.
It depends, basically if you can replicate the derived field with SQL then it can be done (so basically it can be done, but the practicality of it varies). In your example you would need to map your Fullname property like:
then to stop NHibernate from complaining you need to define an empty setter for Fullname:
Then you’ll be able to query Fullname and NHibernate will insert the formula into the sql correctly.