NHibernate:将查询结果映射到映射文件中的列表属性
我有一个 Contact 类,我想为用户提供城市和省份的自动完成功能。
我想我在联系人对象中创建两个集合,并从它们的联系人表中加载城市和省份:
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; }
}
并拥有如下所示的映射文件:
<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>
但这不起作用。有什么办法可以实现这一点吗?
谢谢。
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 you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性上的公式是定义属性值的 SQL 表达式,因此选择应返回单个值。此外,通过按照您建议的方式工作,您将在每个实体上复制这些值,也许最好将其定义为静态列表,并在创建新会话时通过自定义 hql 填充该值。
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.