Question

i'm fairly new to NHibernate and although I'm finding tons of infos on NHibernate mapping on the web, I am too silly to find this piece of information.

So the problem is, i've got the following Model:

Datamodel

this is how I'd like it to look. One clean person that has two Address Properties.

In the database I'd like to persist this in one table. So the Person row would have a ShippingStreetname and a Streetname Column, the one mapped to ShippingAddress.Streetname and the other to Address.StreetName

I found an article on fluent interfaces, but still haven't figured out how to do this through the XML Configuration.

Thanks in advance!

Update: I found the solution to this by myself. This can be done through the node and works rather straightforward.

To achieve the mapping of Address and ShippingAddress I just had to add the following to the

<component name="Address" class="Address">
  <property name="Streetname"></property>
  <property name="Zip"></property>
  <property name="City"></property>
  <property name="Country"></property>
</component>

<component name="ShippingAddress" class="Address">
  <property name="Streetname" column="ShippingStreetname" />
  <property name="Zip" column="ShippingZip" />
  <property name="City" column="ShippingCity" />
  <property name="Country" column="ShippingCountry" />
</component>
Was it helpful?

Solution

Ok. I found the solution myself. The key is the construct in the XML configuration and it works rather nicely.

Here is how it's done:

<component name="Address" class="Address">
  <property name="Streetname"></property>
  <property name="Zip"></property>
  <property name="City"></property>
  <property name="Country"></property>
</component>

<component name="ShippingAddress" class="Address">
  <property name="Streetname" column="ShippingStreetname" />
  <property name="Zip" column="ShippingZip" />
  <property name="City" column="ShippingCity" />
  <property name="Country" column="ShippingCountry" />
</component>

OTHER TIPS

you could configure this as two relations. e.g.

<many-to-one name="ShippingAddress" class="Yournamespace.Address"/>
<many-to-one name="Address" class="Yournamespace.Address"/>

You dont even need an Id for an address. Just think how expensive is to maintain an Id. You have concurrency problems, you need uniqueness, and so on. This is the aim of the ValueObjects (do not get confused with System.ValueObject see DDD definition for ValueObject). In this case Address is a ValueObject so it does not required an Id. And if you need a collection of Address you map it like a "" see http://www.nhforge.org/doc/nh/en/index.html#collections-ofvalues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top