How do I map a one-to-one value type association in an joined-subclass?

Posted by David Rubin on Stack Overflow See other posts from Stack Overflow or by David Rubin
Published on 2010-04-23T21:33:27Z Indexed on 2010/04/29 16:47 UTC
Read the original article Hit count: 225

I've got a class hierarchy mapped using table-per-subclass, and it's been working out great:

class BasicReport
{
  ...
}

class SpecificReport : BasicReport
{
  ...
}

With mappings:

<class name="BasicReport" table="reports">
    <id name="Id" column="id">...</id>
    <!-- some common properties -->
</class>

<joined-subclass name="SpecificReport" table="specificReports" extends="BasicReport">
    <key column="id"/>
    <!-- some special properties -->
</joined-subclass>

So far, so good. The problem I'm struggling with is how to add a property to one of my subclasses that's both a value type for which I have an IUserType implemented and also mapped via an association:

class OtherReport : BasicReport
{
    public SpecialValue V { get; set; }
}

class SpecialValueUserType : IUserType
{
    ...
}

What I'd like to do is:

<joined-subclass name="OtherReport" table="otherReports" extends="BasicReport">
    <key column="id"/>
    <join table="rptValues" fetch="join">
        <key column="rptId"/>
        <property name="V" column="value" type="SpecialValueUserType"/>
    </join>
</joined-subclass>

This accurately reflects the intent, and the pre-existing database schema I'm tied to: the SpecialValue instance is a property of the OtherReport, but is stored in a separate table ("rptValues").

Unfortunately, it seems as though I can't do this, because <join> elements can't be used in <joined-subclass> mappings. <one-to-one> would require creating a class mapping for SpecialValue, which doesn't make any sense given that SpecialValue is just a meaningful scalar.

So what can I do? Do I have any options? Right now I'm playing a game with sets:

class OtherReport : BasicReport
{
     public SpecialValue V
     {
         get { return _values.Count() > 0 ? _values.First() : null; }
         set { _values.Clear(); _values.Add(value); }
     }
     private ICollection<SpecialValue> _values;
}

With mapping:

<joined-subclass name="OtherReport" table="otherReports" extends="BasicReport">
    <key column="id"/>
    <set name="_values" access="field" table="rptValues" cascade="all-delete-orphan">
        <key column="rptId" />
        <element column="value" type="SpecialValueUserType"/>
    </set>
</joined-subclass>

Thanks in advance for the help! I've been banging my head into my desk for several days now.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about hibernate