views:

46

answers:

2

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  A: 

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>
Pedro
Berryl
Unfortunately it's THE mapping :(. As for querying, just go ahead and do a Session.CreateCriteria.For<IWhatever>()
Pedro
@Pedro. It looks like this is the way to go. This [Ayende post](http://ayende.com/Blog/archive/2009/04/10/nhibernate-mapping-ndash-inheritance.aspx) suggests you need to use HiLo for identity when you do this. Another [SO post] backs that up and also suggests a work-around to make it FNH do it. Cheers
Berryl
Diego's answer may suit what I need to do best. There are also apparent other mapping options as nicely outlined in this [SO post](http://stackoverflow.com/questions/2459419/need-help-with-nhibernate-fluent-nhibernate-mapping). Cheers
Berryl
Yes, his answer is indeed what you need.
Pedro
+1  A: 

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.

Diego Mijelshon
nice, I didn't knew you could do that without mapping
Pedro
Implicit polymorphism :-)
Diego Mijelshon
Berryl
Yes, you can. HQL is the same, but you have to `<import/>` the interface (see http://nhforge.org/doc/nh/en/index.html#mapping-declaration-import)
Diego Mijelshon

related questions