Reputation: 6157
I have a file attachment class (FileAttachment) which is used in several other classes.
e.g. An article contains multiple attachments. A category contains multiple attachments. These attachments are actually the same FileAttachment class but for obvious reason they are persisted in different tables. e.g. file attachment for an article in Article_FileAttachments table and category file attachments in a Category_FileAttachments table.
How can i represent this relationship in the nhibernate mappings? thx
Upvotes: 1
Views: 3479
Reputation: 3535
Some options here - I don't know which one will work best for you:
<any>
mapping to do what you want. More info at the nhibernate.info docs and from Ayende. I haven't used this myself, so I'm not entirely sure it will help.<many-to-many>
join.Upvotes: 1
Reputation: 24142
Please see Chapter 8 for polymorphic mapping rules with NHibernate.
In short, you will need a discriminator column to specify what discriminator persists to what table. Here's an instance from the NHibernate documentation, OR if you use inheritance, you will only need to map your derived classes as subclasses and specify the datatable name for each of them from within your base type class mapping.
<class name="IPayment" table="PAYMENT">
<id name="Id" type="Int64" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="Amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
</class>
You may observe that a Payment is a payment, whatever its type of payment. So, it is mapped as a IPayment. Then, subcategorized in multiple tables which represents each type of payment by its discriminator column.
Chapter 8 - Polymorphic mapping
Upvotes: 1