NHibernate version: 1.2.1.4000
Oracle 10g
Examine the following class definitions and mapping files...
Code:
public abstract class UserStatus
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
}
public class InternalUserStatus : UserStatus { }
public class ExternalUserStatus : UserStatus { }
public class User
{
public virtual int Id { get; set; }
public virtual UserStatus Status { get; set; }
}
public class InternalUser : User
{
new public virtual InternalUserStatus Status
{
get { return base.Status as InternalUserStatus; }
set { base.Status = value; }
}
// plus other InternalUser-specific properties
}
public class ExternalUser : User
{
new public virtual ExternalUserStatus Status
{
get { return base.Status as ExternalUserStatus; }
set { base.Status = value; }
}
// plus other ExternalUser-specific properties
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="UserStatus, MyEntityLibrary" table="USER_STATUS" discriminator-value="-1">
<id name="Id" type="Int32" column="ID">
<generator class="native" />
</id>
<property name="Description" type="String" column="DESCRIPTION" />
<discriminator type="integer" column="TYPE_ID" />
<subclass name="InternalUserStatus, MyEntityLibrary" discriminator-value="1" />
<subclass name="ExternalUserStatus, MyEntityLibrary" discriminator-value="2" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="User, MyEntityLibrary" table="USER">
<id name="Id" type="Int32" column="ID">
<generator class="native" />
</id>
<many-to-one name="Status" class="UserStatus, MyEntityLibrary" column="STATUS_ID" />
<joined-subclass name="InternalUser, MyEntityLibrary" table="INTERNAL_USER">
<key column="USER_ID" />
<!-- other child-class specific properties ... -->
</joined-subclass>
<joined-subclass name="ExternalUser, MyEntityLibrary" table="EXTERNAL_USER">
<key column="USER_ID" />
<!-- other child-class specific properties ... -->
</joined-subclass>
</class>
</hibernate-mapping>
I'm trying to use the mapping to place UserStatus instances in the Status property of the base class, User; I then want to access that property from a property that hides it in the sub-classes, InternalUser and ExternalUser. This allows client code using references typed to the sub-classes to access the Status property in a type-safe manner. However, I get the following exception:
Quote:
Invalid mapping information specified for type MyEntityLibrary.InternalUser, check your mapping file for property type mismatches
,
with an inner exception indicating:
Quote:
Unable to cast object of type 'CProxyTypeMyEntityLibraryUserStatusEntityLibrary_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2' to type 'MyEntityLibrary.InternalUserStatus'.
I think NHibernate is getting confused about where to apply the mapping, resulting in setting the wrong property. It may be trying to use the sub-class property instead of the property to which it's actually mapped in the base class. Can anyone shed some light on how I might accomplish this?