0

我在我的应用程序中使用每个子类的表策略进行继承,如 Ayende 在此处的帖子中所述。

但是,当我专门查询子类(例如 Company)并根据 Id(我知道)进行过滤时,生成的 SQL 不正确,并在 SQL Server 中给我一个错误。标准:

session.CreateCriteria<Company>()
   .Add(Expression.Eq("Id", 25)
   .List<Company>();

结果生成的 SQL:

SELECT this_.PartyId,
       this_.CompanyName
FROM   Companies this_
       inner join Parties this_1_
          on this_PartyId = this_1_.Id
WHERE this_1_.PartyId = 25

问题(最后一行 - PartyId 未在Parties 表上定义)是子表中的键列在父表中使用。由于“Id”派生自 C# 中的 Party 类,所以它有点道理。但是为什么它使用键列“PartyId”而不是Party映射中定义的Id“Id”呢?我怎样才能让它发挥作用?

谢谢!

编辑:如要求,这里是映射(与博客文章中的相同)

<class name="Party"
    abstract="true"
    table="Parties">
<id name="Id">
    <generator class="identity"/>
</id>

<joined-subclass
    table="People"
    name="Person">
    <key column="PartyId"/>
    <property name="FirstName"/>
</joined-subclass>

<joined-subclass
    table="Companies"
    name="Company">
    <key column="PartyId"/>
    <property name="CompanyName"/>
</joined-subclass>

4

1 回答 1

0

我终于找到了问题所在。我在映射中犯了一个错误(我使用 Fluent NHibernate 给我你在上面看到的映射)并且我在 Party 类中映射了两次 Id:

public class PartyMap : ClassMap<Party>
{
    public PartyMap()
    {
        Table("Parties");
        Id(p => p.Id).GeneratedBy.Assigned();
        Map(p => p.Id);
    }
}

由于“Id”是被映射的(不是作为Id),当给Company的Id添加where子句时,NHibernate就糊涂了,用键列“PartyId”作为“Id”的映射列,相当混乱!删除 Id 的第二个映射解决了这个问题。

反正我的错!

于 2010-12-15T13:02:55.550 回答