views:

54

answers:

1

Hello, I am using NHibernate for an ecommerce site and am having difficulty getting what should be a simple mapping to work. Essentially I have a ContactDetails entity which is used to save contact details for any type of user - Buyer/Seller/Advertiser/etc. I use an "any" mapping to allow a single table to hold contact_details for each type. The ID is written to contact_details_id and the type (buyer/seller/advertiser) to the contact_details_type field as follows.

<class name="ContactDetails" table="contact_details">

<id name="ID">
  <generator class="hilo" />
</id>

<any name="Party" id-type="System.Int32" meta-type="System.String" cascade="all">
  <meta-value value="buyer" class="Buyer" />
  <meta-value value="seller" class="Seller" />
  <column name="contact_details_type" />
  <column name="contact_details_id" />
</any>

In my buyer mapping I have a one-to-one with contact details which looks as follows

<one-to-one name="ContactDetails" class="ContactDetails" cascade="all" />

This all looks fine and I am able to save contactdetails to the database. My issue is that when I attempt to load a Buyer from the database the ContactDetails is not returned. Rather than joining on the contact_details_id field the generated query joins on the ID field of the contact_details table. I have tried everything possible but no matter what happens I cannot get the code to join on the right field (contact_details_id). Has anyone attempted this and, if so, how did it work? Please let me know if additional detail is rquired.

Thanks in advance,

JP

+1  A: 

one-to-one is not supported for implicit polymorphism.

Alternatives:

  • Create a superclass of Buyer and Seller if you don't have one (you can call it Party), map the hierarchy using any strategy other than implicit, and use a regular one-to-one or many-to-one relationship instead of any (preferred)
  • Don't map the relationship in Buyer and use a query instead

(the limitations of the implicit polymorphism approach are highlighted here: http://nhforge.org/doc/nh/en/index.html#inheritance-limitations)

Diego Mijelshon
Excellent, thanks!
JP