子类 ID 上的 Nhibernate 表每个子类标准使用父表中的键列

发布于 2024-10-07 12:41:27 字数 1279 浏览 4 评论 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>

I using the table per subclass strategy for inheritance in my application, as described in Ayende's post here.

However, when I am querying for a subclass specifically, say Company, and filtering on the Id (which I know), the resulting SQL is incorrect and gives me an error in SQL Server.
The criteria:

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

The resulting generated 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

The problem (last line - PartyId is not defined on the Parties table) is that the key column in the child table is used in the parent table.
Since the "Id" derives from the Party class in C#, it kinda makes sense. But why does it use the key column "PartyId" instead of the Id "Id" defined in the Party mapping? And how can I make it work?

Thanks!

Edit: As asked, here are the mappings (same as the ones in the blog post)

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2024-10-14 12:41:27

我终于找到了问题所在。
我在映射中犯了一个错误(我使用 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),当添加对于公司的 Id 的 where 子句,NHibernate 很困惑,并使用键列“PartyId”作为“Id”的映射列,非常混乱!
删除 Id 的第二个映射解决了该问题。

无论如何,我的错误!

I finally found the problem.
I had made a mistake in the mappings (I was using Fluent NHibernate to give me the mappings you see above) and I mapped twice the Id in the Party class :

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

Since "Id" was mapped (not as an Id), when adding a where clause to the Id of Company, NHibernate was confused and used the key column "PartyId" as the mapped column for "Id", quite confusing!
Removing the second mapping for Id solved the problem.

Anyway, my mistake!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文