开发者

Mapping NHibernate many-to-one relationships from abstract base classes to union-subclasses

I am having a problem mapping a many-to-one relationship from an abstract base class mapping to a concrete union-subclass. Example:

public abstract class Entity
{
    public virtual Guid ID {get; set;}
    public virtual string Name {get;set;}
    public virtual User OwnerUser {get; set;}
}

public class User : Entity
{
    public virtual string UserName {get; set;}
}

As you can see here, I have a base abstract class for all of my database objects. I am mapping these classes with the Entity class as the abstract mapping class and the User as a union-subclass. When creating the configuration object, no errors are thrown and the Schema exports just fine. However, the field to the OwnerUser just won't show up in the database for all of the concrete classes. Here is an example of how the mapping looks

<class entity-name="Entity" name="Entity" abstract开发者_运维技巧="true">
    <id name="ID" type="guid">
       <generator class="guid.comb"/>
    </id>
    <property name="Name" />
    <many-to-one name="OwnerUser" column="ID" entity-name="User" />
</class>

<union-subclass name="User" entity-name="User" extends="Entity">
    <property name="UserName" />
</union-subclass>

I am also using an Oracle XE instance as the database backend. If this isn't enough information to properly answer the question, let me know and I will add what I can.

Worst case scenario I'll just add the many-to-one relationship on all of the concrete objects explicitly, but this isn't the most elegant solution and I think what I've proposed should work. I have been unable to find any concrete examples of this on the tubes that power the various internets, nor have I found anything in the documentation explicitly stating this is an invalid use case, although I could have missed something. It wouldn't have been the first time :-/

Any answers to this question are greatly appreciated. Thank you for your time.


Wow, just... wow.

Instead of attempting to delete this post, I will leave it up for another lowly nhibernate noob to find one day, as I couldn't find any questions around this specific use case.

The fact is that you can do this with no problem. My problem was that I was specifying ID for the column attribute in the many-to-one mapping element. For some odd reason, I thought that was the column that the foreign key was referencing (which is actually the property-ref, if I'm not mistaken). That attribute actually specifies the column name in the table that holds the reference (d'oh!). Surprisingly, NHibernate will happily map multiple mapping elements to the same column name without a warning. I'm not trying to blame them, perhaps the use case for that is allowing some type of override on a polymorphic derived class.

Anyway, this is a valid use case and changing the column attribute to the name of the column in the referring table worked fine. Perhaps I've been looking at bits and bytes for a little too long each night this week... sigh.

Thank you to everyone who took the time to check out this post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜