nhibernate query all objects implementing an interface

最后都变了- 提交于 2019-12-30 10:34:19

问题


For example, if you have an Apple : IWhatever, and an Orange : IWhatever and you want to find both of them because they are IWhatevers, what do you need to do in NHibernate?

Is it completely dependent on the HQL or criteria query, or is there something you must do in the mapping also? If there is a mapping requirement, can Fluent NHibernatee support it?


回答1:


You could do a UnionSubClass mapping. Unforntunately it is unsuported in Fluent.

You mapping would be something like:

<class name="IWhatever" abstract="true">
    <id name="Id">
    </id>

    <union-subclass name="Apple">
        <property name="Bla" type="int"/>
    </union-subclass>

    <union-subclass name="Orange">
        <property name="Bla" type="int"/>
    </union-subclass>
</class>



回答2:


For Criteria, you don't need to map it. Just:

session.CreateCriteria<IWhatever>()
       .List<IWhatever>();

Keep in mind you'll only be able to query/sort/project on fields that exist in IWhatever.



来源:https://stackoverflow.com/questions/3612816/nhibernate-query-all-objects-implementing-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!