【问题标题】:NHibernate: could not initialize a collection:NHibernate:无法初始化集合:
【发布时间】:2014-05-11 15:46:15
【问题描述】:

我正在努力使用 NHibernate。我有一个小的 ASP MVC 网站。我设法设置了 NHibernate,但是当我想检索数据时,我收到了这个错误:

附加信息:无法初始化集合:[Lore.Models.DatabaseModels.Statue.Keywords#1] [SQL:选择keywords0_.IdStatueKeyword 作为IdStatue1_1_,keywords0_.IdKeyword 作为IdKeyword1_,keyword1_.IdKeyword 作为IdKeyword4_0_,keyword1_.Name 作为Name4_0_, 关键字1_.描述为Descript8_4_0_ FROM StatueKeyword 关键字0_ 左外连接 Statuekeyword1_ 在关键字0_.IdKeyword=keyword1_.IdKeyword WHERE 关键字0_.IdStatueKeyword=?]

我也不确定我是否正确实施了多对多关系。这是我的表结构:

雕像

  • IdStatue
  • 姓名

关键字

  • IdKeyword
  • 姓名

StatueKeyword

  • IdStatueKeyword
  • IdStatue
  • IdKeyword

雕像类:

public class Statue
{
    public Statue()
    {
        Keywords = new List<Keyword>();
    }

    public int IdStatue { get; set; }
    public string Name { get; set; }

    public IList<Keyword> Keywords { get; set; }
}

关键字类

public class Keyword
{
    public int IdKeyword { get; set; }
    public string Name { get; set; }
}

雕像 hbm 文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Statue" table="Statue" lazy="false" >
<id name="IdStatue" column="IdStatue">
  <generator class="identity" />
</id>

<property name="Name" column="Name" not-null="true" type="System.String" />

<bag name="Keywords" table="StatueKeyword" lazy="false">
  <key column="IdStatueKeyword"/>
  <many-to-many class="Keyword" column="IdKeyword"/>
</bag>

</class>
</hibernate-mapping>

关键字 hbm 文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Lore.Models.DatabaseModels" assembly="Lore">
<class name="Keyword" table="Statue" lazy="false" >

<id name="IdKeyword" column="IdKeyword">
  <generator class="identity" />
</id>

<property name="Name" column="Name" not-null="true" type="System.String" />
<property name="Description" column="Description" not-null="true" type="System.String" />

</class>
</hibernate-mapping>

我需要为硕士论文制作这个网站,因此非常感谢任何帮助!

【问题讨论】:

    标签: asp.net-mvc nhibernate many-to-many nhibernate-mapping


    【解决方案1】:

    问题应该/可能在&lt;key&gt; 映射中:

    <bag name="Keywords" table="StatueKeyword" lazy="false">
      <!-- <key> is representing column where current Statue ID should be searched 
       while the below one seems to be the ID column of the pairing table
       so instead of this
      <key column="IdStatueKeyword"/>
       use this: -->
      <key column="IdStatue"/>
      <many-to-many class="Keyword" column="IdKeyword"/>
    </bag>
    

    还要检查这些:

    小引:

    从集合表到所属类表的外键是使用&lt;key&gt; 元素声明的。

    另一个提示,如果您确实有配对表的 ID 列,您应该尝试使用增强功能:

    另一个关于 idbag 的文档引用:

    请注意,&lt;idbag&gt; 的更新性能比普通的&lt;bag&gt; 好得多! NHibernate 可以有效地定位单个行并单独更新或删除它们,就像列表、映射或集合一样。

    最后,我(个人)劝阻你不要使用many-to-many。我的观点是,最好避免它。见:24. Best Practices(引用:)

    不要使用奇异的关联映射。

    真正多对多关联的良好用例很少见。大多数时候,您需要存储在“链接表”中的附加信息。在这种情况下,最好使用两个一对多关联到一个中间链接类。事实上,我们认为大多数关联都是一对多和多对一的,在使用任何其他关联方式时你应该小心,问问自己是否真的有必要。

    也许以后还要检查这些:

    【讨论】:

    • 感谢您的回答!我决定将我的关系改为一对多。在这种情况下,这确实是正确的方法。
    • 如果有帮助的话就太好了。享受 NHibernate ;)
    • 抱歉,我也想尝试多对多关系,但似乎无法正常工作。我像你说的那样调整了我的 xml 文件,但仍然得到同样的错误。有任何想法吗?非常感谢!
    • 好的,我修好了,有一个复制粘贴错字。
    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2011-04-04
    • 1970-01-01
    相关资源
    最近更新 更多