为什么我的 IList是作为空对象列表返回?

发布于 2024-10-09 12:16:41 字数 1998 浏览 0 评论 0原文

虽然我意识到 Set 或 Bag 可能是执行此操作的正确方法,但我是 NHibernate 的新手,我试图理解为什么会发生以下情况。

我有两个类:

public class Customer
{
  public virtual int Id { get; protected set; }
  public virtual string CustomerName { get; set; }
  // Customer has many domains
  public virtual IList<Domain> Domains { get; set; }
}

public class Domain
{
  public virtual int Id { get; protected set; }
  public virtual int CustomerID { get; set; }
  public virtual string DomainName { get; set; }
}

我的映射文件如下所示:

<!-- Domain -> tblDomains           -->           
<class name="Domain" table="tblDomains">

  <id name="Id">
    <column name="DomainID" sql-type="int" not-null="true"/>
    <generator class="identity"/>
  </id>

  <property name="CustomerID"/>
  <property name="DomainName"/>

</class>

<!-- Customer -> tblCustomer       -->
<class name="Customer" table="tblCustomer">

  <id name="Id">
    <column name="CustomerID" sql-type="int" not-null="true"/>
    <generator class="identity"/>
  </id>

  <property name="CustomerName" column="Customer"/>

  <list name="Domains">
    <key column="CustomerID"/>
    <index column="DomainID"/>
    <one-to-many class="Domain" />
  </list>

</class>

当我检索 Customer 对象时,Domains 属性包含 665383 个 null Domain 对象的列表。列表中的第 665384 项包含有效的填充对象。

只有 63 个属于该客户,因此我猜测这是某种笛卡尔积结果。我查看了 NHProfiler 中的 SQL,但当我迭代 Domains 列表中的第一项时,我看到的只是一个看起来相当无辜的查询

SELECT domains0_.CustomerID  as CustomerID1_,
       domains0_.DomainID    as DomainID1_,
       domains0_.DomainID    as DomainID2_0_,
       domains0_.CustomerID  as CustomerID2_0_,
       domains0_.DomainName  as DomainName2_0_
FROM   tblDomains domains0_
WHERE  domains0_.CustomerID = 5667 /* @p0 */

: 这一切都工作得很好。谁能解释一下引擎盖下发生了什么?

Whilst I realise that a Set or Bag is probably the correct way to do this, I'm new to NHibernate and I'm trying to understand why the following is happening.

I have two classes:

public class Customer
{
  public virtual int Id { get; protected set; }
  public virtual string CustomerName { get; set; }
  // Customer has many domains
  public virtual IList<Domain> Domains { get; set; }
}

public class Domain
{
  public virtual int Id { get; protected set; }
  public virtual int CustomerID { get; set; }
  public virtual string DomainName { get; set; }
}

My mapping files look like:

<!-- Domain -> tblDomains           -->           
<class name="Domain" table="tblDomains">

  <id name="Id">
    <column name="DomainID" sql-type="int" not-null="true"/>
    <generator class="identity"/>
  </id>

  <property name="CustomerID"/>
  <property name="DomainName"/>

</class>

<!-- Customer -> tblCustomer       -->
<class name="Customer" table="tblCustomer">

  <id name="Id">
    <column name="CustomerID" sql-type="int" not-null="true"/>
    <generator class="identity"/>
  </id>

  <property name="CustomerName" column="Customer"/>

  <list name="Domains">
    <key column="CustomerID"/>
    <index column="DomainID"/>
    <one-to-many class="Domain" />
  </list>

</class>

When I retrieve a Customer object the Domains property contains a list of 665383 null Domain objects. The 665384'th item in the list contains a valid populated object.

There are only 63 Domain's that belong to this customer so I'm guessing this is some kind of cartesian product result. I've peeked at the SQL in NHProfiler but all I see is a query that looks fairly innocent when I iterate over the first item in the Domains list:

SELECT domains0_.CustomerID  as CustomerID1_,
       domains0_.DomainID    as DomainID1_,
       domains0_.DomainID    as DomainID2_0_,
       domains0_.CustomerID  as CustomerID2_0_,
       domains0_.DomainName  as DomainName2_0_
FROM   tblDomains domains0_
WHERE  domains0_.CustomerID = 5667 /* @p0 */

If I use a <bag> this all works just fine. Can anyone explain what's going on under the bonnet?

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

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

发布评论

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

评论(1

战皆罪 2024-10-16 12:16:41

对于列表映射,索引适用于列表中的对象集。也就是说,如果客户有一组 63 个域,则通常该列表将包含从 0 到 62 的值,以指示该客户的域集中的域对象的索引。

您已将索引设置为表的主键 DomainId,这造成了严重破坏。我猜测 Domain 表总共有 665384 行(如果 DomainId 不以 1 开头并且有间隙,则行数更少),但我认为 63 行有效,而不是 1 行。你都检查了吗? :-)

With a list mapping, the index applies to the set of objects in the list. That is, if a Customer has a set of 63 Domains then normally the list would contain values from 0 to 62 to indicate the index of the Domain object in that Customer's set of Domains.

You have set the index to DomainId, the primary key of the table, which is wreaking havoc. I would guess that the Domain table has 665384 rows total (or fewer if DomainId doesn't start with 1 and has gaps), but I would think that 63 would be valid instead of one. Did you check them all? :-)

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