开发者

C# NHibernate Joined-Subclass Get Problem

I have the following mapped classes:

    <class name="Company" table="Company">
        <id name="ID" column="CompanyID">
          <generator class="native"/>
        </id>
        <joined-subclass name="CompanyAccounting" table="company_accounting" lazy="true">
          <key column="CompanyID"/>
        </joined-subclass>
        <joined-subclass name="CompanyCarbon" table="company_carbon" lazy="true">
          <key column="CompanyID" />
        </joined-subclass>
    </class>

Now the following test fails:

        var company = CompanyAdapter.Get<Company>(_session, 32);
        Assert.IsNotNull(company);

        var client = CompanyAdapter.Get<CompanyClient>(_session, 32);
        Assert.IsNotNull(client);

But the following succeeds:

        var client = CompanyAdapter.Get<CompanyClient>(_session, 32);
        Assert.IsNotNull(client);

        var company = CompanyAdapter.Get<Company>(_session, 32);
        Assert.IsNotNull(co开发者_如何学Gompany);

The reason the first test fails is because NHibernate retrieve Company as CompanyAccounting, as Company(32) is both an accounting company and a client, and when i call Session.Get after it has retrieved company as CompanyAccounting it doesn't go back to the database and check if there is an entry for CompanyClient, it assumes Company can only be on subclass, is there anyway of making it specify that it can inherit from all of the subclasses?


What i ended up doing was refactoring my classes, and rather than having

class Company, class CompanyClient : Company, class CompanyAccounting : Company

I made CompanyClient and CompanyAccounting not inherit from Company, and added Company as a <one-to-one> relation which is the primary key for those classes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜