问题
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