NHibernate - 如何使用 hbm 文件映射从数据库获取条件数据
目前我正在 hbm 文件中使用 多对一 元素从数据库中获取数据对象,如下所示......
<property name="ContactId" length="4" />
<many-to-one
name="DefaultContact"
column="ContactId"
class="Models.Contact"
update="false"
insert="false"/>
此代码正确获取数据,但现在我需要有条件地获取数据就像我在 hbm 文件中具有以下属性和 mant-to-one 元素......
<property name="ParentId" length="4" />
<property name="ParentType" length="4" />
<many-to-one
name="ContactParent"
column="???????? ParentId which could be CustomerId or ProspectId or LeadId according to Parent Type ????????"
class="???????? Models.Customer or Models.Prospect or Models.Lead - according to Parent Type ????????"
update="false"
insert="false"/>
并且我必须根据“Parent Type”属性中的值获取数据,这意味着我需要设置“many”的类属性-to-one”元素根据“父类型”动态变化 财产。
那么现在,我怎样才能通过多对一元素或其他方式达到预期的结果......?
提前致谢。
Currently I am using many-to-one element in hbm file to fetch the data object from database like following....
<property name="ContactId" length="4" />
<many-to-one
name="DefaultContact"
column="ContactId"
class="Models.Contact"
update="false"
insert="false"/>
This code is fetching the data properly, but now I need to fetch the data conditionally like I am having the following properties and mant-to-one element in hbm file.....
<property name="ParentId" length="4" />
<property name="ParentType" length="4" />
<many-to-one
name="ContactParent"
column="???????? ParentId which could be CustomerId or ProspectId or LeadId according to Parent Type ????????"
class="???????? Models.Customer or Models.Prospect or Models.Lead - according to Parent Type ????????"
update="false"
insert="false"/>
And I have to fetch the data according to value in "Parent Type" property, which means I need to set class attribute of "many-to-one" element dynamically according to "Parent Type"
property.
So now, how can I achieve the desired result with many-to-one element or some other way...?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该查看
映射 http://nhibernate.info/doc/nh/en/index.html#mapping-types-anymapping这是 NHibernate 提供的最接近您想要做的事情。
You should look into the
<any>
mapping http://nhibernate.info/doc/nh/en/index.html#mapping-types-anymappingIt's the closest to what you want to do that NHibernate offers.
我可以想到两种方法来处理这个问题。
不要在模型中包含 ContactParent,使用单独的方法从 ParentType 和 ParentId 检索它。
不要在模型
将所有三个父类型映射为私有成员,并返回公共属性中不为 null 的成员。
选项 1 将是我的第一选择。
I can think of two ways to handle this.
Don't include ContactParent in the model, use a separate method to retrieve it from ParentType and ParentId.
Map all three parent types as private members and return the one that's not null in a public property.
Option 1 would be my first choice.