Nathan
Nathan

Reputation: 10784

NHIbernate: How to map a bag with an ICompositeUserType

Suppose I have a class with a collection of immutable types, that I would like to map with a custom ICompositeUserType

public class Foo
{
    public long Id { get; protected set; }

    public virtual IList<Bar> Bars { get; set; }
}

//immutable
public class Bar
{
    private _blah;
    private _halb;

    public Bar(string blah, string halb)
    {
        _blah = blah; _halb = halb;
    }

    public Blah { get { return _blah; } }
    public Halb { get { return _halb; } }
}

How do I set up the mapping of the bag on Foo to use a composite user type, so that the custom type can invoke the constructor when necessary?

I tried this, but type isn't available on composite-element.

<bag name="Bars" table="FooBars" lazy="true">
   <key column="FooId" foreign-key="FK_Bars_FooId" />
   <composite-element class="Doman.Bar, Domain" type="Integration.UserTypes.BarCompositeUserType, Integration">
       <property name="Blah">
          <column name ="BarBlah" sql-type="char(2)"/>
       </property>
   </composite-element>
</bag>

However, that won't work because NHibernate does not recognize the "type" attribute on "composite-element"

How to I map a bag with an ICompositeUserType? (I am using NHibernate 3.2)

Upvotes: 2

Views: 417

Answers (1)

Firo
Firo

Reputation: 30813

element instead of composite-element because the properties and conversion is specified inside the ICompositeUserType

<element type="Integration.UserTypes.BarCompositeUserType, Integration">
  <column name="BarBlah" />
  <column name="BarHalb" />
</element>

Upvotes: 2

Related Questions