Nhibernate 一对多,每个子类有一个表

发布于 2024-08-27 20:52:39 字数 2268 浏览 8 评论 0原文

我正在定制 N2CMS 的数据库结构,遇到了一个问题。下面列出了这两个类。

public class Customer : ContentItem
{
  public IList<License> Licenses { get; set; }
}

public class License : ContentItem
{
  public Customer Customer { get; set; }
}

nhibernate 映射如下。

  <class name="N2.ContentItem,N2" table="n2item">
    <cache usage="read-write" />
    <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
      <generator class="native" />
    </id>
    <discriminator column="Type" type="String" />
  </class>

  <subclass name="My.Customer,My" extends="N2.ContentItem,N2" discriminator-value="Customer">
    <join table="Customer">
      <key column="ItemID" />
      <bag name="Licenses" generic="true" inverse="true">
        <key column="CustomerID" />
        <one-to-many class="My.License,My"/>
      </bag>
    </join>
  </subclass>

  <subclass name="My.License,My" extends="N2.ContentItem,N2" discriminator-value="License">
    <join table="License" fetch="select">
      <key column="ItemID" />
      <many-to-one name="Customer" column="CustomerID"  class="My.Customer,My" not-null="false" />
    </join>
  </subclass>

然后,当获取 Customer 实例时,customer.Licenses 始终为空,但实际上数据库中有该客户的许可证。当我检查nhibernate日志文件时,我发现SQL查询如下:

SELECT   licenses0_.CustomerID     as CustomerID1_,
         licenses0_.ID             as ID1_,
         licenses0_.ID             as ID2_0_,
         licenses0_1_.CustomerID   as CustomerID7_0_,
FROM     n2item licenses0_
         inner join License licenses0_1_
           on licenses0_.ID = licenses0_1_.ItemID
WHERE    licenses0_.CustomerID = 12 /* @p0 */

看来nhibernate认为CustomerID在'n2item'表中。我不知道为什么,但为了让它工作,我认为 SQL 应该是这样的。

SELECT   licenses0_.ID             as ID1_,
         licenses0_.ID             as ID2_0_,
         licenses0_1_.CustomerID   as CustomerID7_0_,
FROM     n2item licenses0_
         inner join License licenses0_1_
           on licenses0_.ID = licenses0_1_.ItemID
WHERE    licenses0_1_.CustomerID = 12 /* @p0 */

任何人都可以指出我的映射出了什么问题吗?我怎样才能获得一位客户的正确许可证?提前致谢。

I am customizing N2CMS's database structure, and met with an issue. The two classes are listed below.

public class Customer : ContentItem
{
  public IList<License> Licenses { get; set; }
}

public class License : ContentItem
{
  public Customer Customer { get; set; }
}

The nhibernate mapping are as follows.

  <class name="N2.ContentItem,N2" table="n2item">
    <cache usage="read-write" />
    <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
      <generator class="native" />
    </id>
    <discriminator column="Type" type="String" />
  </class>

  <subclass name="My.Customer,My" extends="N2.ContentItem,N2" discriminator-value="Customer">
    <join table="Customer">
      <key column="ItemID" />
      <bag name="Licenses" generic="true" inverse="true">
        <key column="CustomerID" />
        <one-to-many class="My.License,My"/>
      </bag>
    </join>
  </subclass>

  <subclass name="My.License,My" extends="N2.ContentItem,N2" discriminator-value="License">
    <join table="License" fetch="select">
      <key column="ItemID" />
      <many-to-one name="Customer" column="CustomerID"  class="My.Customer,My" not-null="false" />
    </join>
  </subclass>

Then, when get an instance of Customer, the customer.Licenses is always empty, but actually there are licenses in the database for the customer. When I check the nhibernate log file, I find that the SQL query is like:

SELECT   licenses0_.CustomerID     as CustomerID1_,
         licenses0_.ID             as ID1_,
         licenses0_.ID             as ID2_0_,
         licenses0_1_.CustomerID   as CustomerID7_0_,
FROM     n2item licenses0_
         inner join License licenses0_1_
           on licenses0_.ID = licenses0_1_.ItemID
WHERE    licenses0_.CustomerID = 12 /* @p0 */

It seems that nhibernate believes that the CustomerID is in the 'n2item' table. I don't know why, but to make it work, I think the SQL should be something like this.

SELECT   licenses0_.ID             as ID1_,
         licenses0_.ID             as ID2_0_,
         licenses0_1_.CustomerID   as CustomerID7_0_,
FROM     n2item licenses0_
         inner join License licenses0_1_
           on licenses0_.ID = licenses0_1_.ItemID
WHERE    licenses0_1_.CustomerID = 12 /* @p0 */

Could any one point out what's wrong with my mappings? And how can I get the correct licenses of one customer? Thanks in advance.

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

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

发布评论

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

评论(1

落叶缤纷 2024-09-03 20:52:39

我不确定 SQL 是否不正确,因为父类映射使用鉴别器,所以我希望所有属性都存储在与基类 (n2item) 相同的表中。但是我不熟悉“连接表”语法,我通常使用连接子类,所以我可能会误解。

假设子类映射是正确的,许可证的问题是否可能与没有为该集合设置级联设置有关?

I'm not sure whether the SQL is incorrect, because the parent class mapping uses a discriminator so I'd expect all properties to be stored in the same table as the base class (n2item). However I'm not familiar with the "join table" syntax, I generally use joined-subclass so I might be misunderstanding.

Assuming the subclass mapping is correct, could the problem with the licenses be something to do with no Cascade setting being set for that collection?

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