mapping different addresses with nhibernate
I've been using nhibernate for a few months now and I am starting to be confident with it but there are still lots of things that I need to explore.
Till now I've mapped addresses as components. Here's an example: <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Lead" table="Leads">
<id name="Code" type="System.Guid">
<column name="LeadCode" />
<generator class="guid.comb" />
</id>
<property name="FirstName">
<column name="FirstName" length="40" not-null="true" />
</property>
<property name="LastName">
<column name="LastName" length="40" not-null="true" />
</property>
<component name="PrimaryAddress" class="Address">
<property name="Street" type="AnsiString">
<column name="PrimaryStreet" length="100" />
</property>
<property name="City">
<column name="PrimaryCity" length="30" />
</property>
<property name="State">
<column name="PrimaryState" length="20" />
</property>
<property name="PostalCode">
<column name="PrimaryPostalCode" length="10" />
</property>
<property name="Country">
<column name="PrimaryCountry" length="40"开发者_运维技巧 />
</property>
</component>
<component name="AlternativeAddress" class="Address">
<property name="Street">
<column name="AlternativeStreet" length="100" />
</property>
<property name="City">
<column name="AlternativeCity" length="30" />
</property>
<property name="State">
<column name="AlternativeState" length="20" />
</property>
<property name="PostalCode">
<column name="AlternativePostalCode" length="10" />
</property>
<property name="Country">
<column name="AlternativeCountry" length="40" />
</property>
</component>
</class>
</hibernate-mapping>
Now, I would like to extend this model and separate the addresses in a different table so that one Lead can have different types of addresses.
I would like - possibly - to use an enum to manage different types of addresses. Every help or link to documents where I can find more infos would be appreciated.Since you've mapped addresses as components, you're probably treating them as value types. If you want to keep them as value types, then you probably need to create an intermediate entity between your lead and your address value (could be called LeadAddress) which contains the Enum designating the type of address, and the Address itself which is the value type, and of course the reference back to the Lead if you want to have a bi-directional relationship. Your lead can then have a collection of "LeadAddress" as a one to many relationship.
Mapping enums is answered here: How to persist an enum using NHibernate
精彩评论