NHibernate - mapping classes that implement multiple interfaces
I am using the Table Per Subclass strategy to persist an inheritance hierarchy:
alt text http://dl.dropbox.com/u/1563210/budget%20obj%20graph.jpg
I am running into some confusion as to how to map the Debit class, which implements 2 interfaces. I may be overthinking it; I'm still learning NH.
Thanks for any input.
EDIT
What's confusing me is that the only properties that my concrete classes have, the开发者_C百科y are getting from those interfaces. This is why I think I will have to map the Interfaces along with the concrete classes.
NHibernate doesn't know or care about the interfaces. You can't, for example, use NHibernate to query for objects that implement a specific interface. You have a table-per-class mapping so you should map it with subclass declarations as described in the documentation.
I'm very new to NHibernate myself but have done something similar so hopefully if I post something along the right lines It'll give us a start so you can comment and in the spirit of stackoverflow someone can edit my answer perhaps.
I think basically the hbm needs to use "subclass", "joined-subclass" or "union-subclass" to achieve what you're looking for.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="YourAssembly.Bll"
namespace="YourAssembly.Bll.Domain">
<class name="Transaction" table="Transaction">
<id name="Id">
<generator class="native"/>
</id>
<property name="Amount" not-null="true" />
<property name="Currency"/>
<joined-subclass name="Debit" table="DebitTransaction">
<key column="TransactionId"/>
<property name="CardHolderName" not-null="true" />
</joined-subclass>
</class>
</hibernate-mapping>
The Hibernate docs on inheritance are very useful
精彩评论