This is NH specific, but anyway….please cross post on nhusers

 

A solution is to use a formula in the ‘composite-element mapping’ for the 
‘duplicated’ property

 

In FNH lingo (to avoid magic strings) inside component mapping

 

c.Map(x => x. RegionName).Formula(ReflectionHelper.GetProperty<Money>(x => x. 
RegionName).Name);

 

=>

 

<property name="RegionName" column=” RegionName” formula=" RegionName " />

 

Inside parent ‘composite-element’ element

 

HTH

Morten

 

  _____  

From: Martin Nilsson [mailto:mffmar...@gmail.com] 
Sent: Saturday, December 05, 2009 1:32 PM
To: fluent-nhibernate@googlegroups.com
Subject: [fluent-nhib] Mapping a dictionary with components

 

Hi,

I have a dictionary field in my Product class that contains prices (Money 
class) per region. In other words, a dictionary containg Value Objects.

I can save the prices with correct region and amount but when I read them from 
the database then I can't get the region name (Money.RegionName) to map 
correctely. The Money.Amount is mapped correctely though. This is my setup. 

public class ProductMap : ClassMap<Product>
{
  public ProductMap()
  {
    Id(x => x.Id);
    HasMany(x => x.Prices).Access.CamelCaseField().Cascade.All().AsMap(key => 
key.RegionName)
      .Component(desc => desc.Map(x => x.Amount));
  }
}

public sealed class Money : IEquatable<Money>, IComparable, IComparable<Money>
{
    private readonly string regionName;
    private readonly decimal amount;

    public Money()
        : this(0, new RegionInfo(CultureInfo.CurrentCulture.LCID))
    {
    }
    public Money(decimal amount, string regionName) : this(amount, new 
RegionInfo(regionName))
    {
    }

    public string RegionName
    {
        get { return regionName; }
    }

    public decimal Amount
    {
        get { return amount; }
    }
}

Map part in hbm file:
<map access="field.camelcase" cascade="all" name="Prices" mutable="true">
  <key>
    <column name="Product_id" />
  </key>
  <index type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089">
    <column name="RegionName" />
  </index>
  <composite-element class="Money, Domain, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=null">
    <property access="field.camelcase" name="Amount" type="System.Decimal, 
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Amount" />
    </property>
  </composite-element>
</map>

If I change to this (adding map of region name in component):

HasMany(x => x.Prices).Access.CamelCaseField().Cascade.All().AsMap(key => 
key.RegionName)
  .Component(desc =>
    {
        desc.Map(x => x.Amount).Access.CamelCaseField();
        desc.Map(x => x.RegionName).Access.CamelCaseField();
    });
    
Then I get error: 
[MappingException: Repeated column in mapping for collection: Product.Prices 
column: RegionName]
   NHibernate.Mapping.Collection.CheckColumnDuplication(ISet`1 distinctColumns, 
IEnumerable`1 columns) in d:\OSS\NHibernate 
2.1\nhibernate\src\NHibernate\Mapping\Collection.cs:615

I have searched for dictionary with components examples but only found for set.
For example "7.2. Collections of dependent objects", 
https://www.hibernate.org/hib_docs/nhibernate/html/components.html

Any hints?

--

You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

--

You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.


Reply via email to