我在我的应用程序中使用每个子类的表策略进行继承,如 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>