NHibernate: Mapping query results to a list property in mapping file
I have a Contact class and I want to provide the users with AutoComplete for City and Province.
I Thought I create two collections in the Contact object and load the Cities and Provinces from the contact table on them:
public class Contact {
public virtual string Name { get; set; }
public virtual string City { get; set; }
public virtual string Province { get; set; }
public virtual IList<String> Cities { get; set; }
public virtual IList<String> Provinces { get; set; }
}
And have the mapping file like this:
<class name="Contact">
<id name="Id">
<generator class="native">
<param name="sequence">contact_id_seq</param>
</generator>
</id>
<property name="Name" />
<property name="City" />
<property name="Province" />
<property name="Cities" type="string" formula="SELECT DISTINCT city FROM contact" />
<property name="Provinces" type="string" formula="SELECT DISTINCT province FROM contact" />
</class>
But this does not work. Is there any way this can be accomplished?
Thank开发者_Python百科 you.
formula on property is an SQL expression that defines the value for the property, so the select should return a single value. In addition by working in the way you proposed you will duplicate those values on each entity, maybe is better to define it as a static list and fill the value by a custom hql when, for example, a new session is created.
精彩评论