Hibernate version: 1.2.0.CR1
Is it possible to map a generic
dictionary of
value objects (composite-elements) where the
key is a property from the composite-element?
This is the mapping for the dictionary:
Code:
<map name="Values" table="CurrencyPrice" cascade="all-delete-orphan" access="nosetter.camelcase-underscore">
<key column="PriceId" />
<index-many-to-many column="CurrencyId" class="MyApplication.Domain.Currency, MyApplication.Domain" />
<composite-element class="MyApplication.Domain.Money, MyApplication.Domain">
<property
name="Amount"
column="Value"
type="Decimal"
access="nosetter.camelcase-underscore" />
<many-to-one
name="Currency"
column="CurrencyId"
class="MyApplication.Domain.Currency, MyApplication.Domain"
access="nosetter.camelcase-underscore"
not-null="true" />
</composite-element>
</map>
As you can see, I'm using the column CurrencyId
twice - as the dictionary's key and as a property of the composite-element "Money".
I get the following NHibernate.MappingException when building the SessionFactory:
Quote:
Repeated column in mapping for collection: MyApplication.Domain.Price.Values column: CurrencyId
Type: NHibernate.MappingException
Source: NHibernate
TargetSite: Void CheckColumnDuplication(Iesi.Collections.ISet, System.Collections.ICollection)
HelpLink: null
Stack: at NHibernate.Persister.Collection.AbstractCollectionPersister.CheckColumnDuplication(ISet distinctColumns, ICollection columns)
...
Mapping documents:Price.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="save-update">
<class name="MyApplication.Domain.Price, MyApplication.Domain" table="Price" lazy="true">
<id name="Id" column="Id" type="Int32" unsaved-value="0" access="nosetter.camelcase-underscore">
<generator class="identity" />
</id>
<map name="Values" table="CurrencyPrice" cascade="all-delete-orphan" access="nosetter.camelcase-underscore">
<key column="PriceId" />
<index-many-to-many column="CurrencyId" class="MyApplication.Domain.Currency, MyApplication.Domain" />
<composite-element class="MyApplication.Domain.Money, MyApplication.Domain">
<property
name="Amount"
column="Value"
type="Decimal"
access="nosetter.camelcase-underscore" />
<many-to-one
name="Currency"
column="CurrencyId"
class="MyApplication.Domain.Currency, MyApplication.Domain"
access="nosetter.camelcase-underscore"
not-null="true" />
</composite-element>
</map>
<!-- SNIP -->
</class>
</hibernate-mapping>
Currency.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="save-update">
<class name="MyApplication.Domain.Currency, MyApplication.Domain" table="Currency" lazy="true">
<id name="Id" column="Id" type="Int32" unsaved-value="0" access="nosetter.camelcase-underscore">
<generator class="identity" />
</id>
<property
name="Code"
column="CurrencyCode"
type="String"
access="nosetter.camelcase-underscore"
not-null="true" />
<!-- SNIP -->
</class>
</hibernate-mapping>
Domain objects:Value object "Money":
Code:
struct Money {
decimal Amount { get; }
Currency Currency { get; }
}
Entity "Currency":
Code:
class Currency {
int Id { get; }
string Code { get; } // "EUR", "USD" etc.
// ...
}
The Price object holds prices for a product in different currencies. To make it easy to get the price of a product in a given currency, I would like to have a dictionary of the
prices:
Code:
class Price {
Dictionary<Currency, Money> Values { get; }
}