我的NHibernate书有错吗?

发布于 2024-10-09 18:39:42 字数 1917 浏览 0 评论 0 原文

我开始学习 NHibernate (3.0) 并拿起了 Packt Publishing 的 的副本NHibernate 3.0 食谱

我正在浏览一个关于一对多映射的部分,但使用的是我自己的数据库。它建议我应该做这样的事情来建模客户及其域之间的一对多关系:

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

客户映射:

<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"/>
    <one-to-many class="Domain" />
  </list>
</class>

当我运行此命令时,我收到以下错误:

XML验证错误:命名空间“urn”中的元素“list” :nhibernate-mapping-2.2' 在命名空间 'urn:nhibernate-mapping-2.2' 中具有无效的子元素 'one-to-many'。预期可能元素的列表:命名空间“urn:nhibernate-mapping-2.2”中的“index, list-index”。

本书的示例有点复杂,因为它们使用每个子类的表映射:

alt text

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Eg.Core"
    namespace="Eg.Core">
  <subclass name="Movie" extends="Product">
    <property name="Director" />
    <list name="Actors" cascade="all-delete-orphan">
      <key column="MovieId" />
      <index column="ActorIndex" />
      <one-to-many class="ActorRole"/>  <-- Is this wrong?
    </list>
  </subclass>
</hibernate-mapping>

我猜这本书是错的?

I'm starting to learn NHibernate (3.0) and picked up a copy of Packt Publishing's NHibernate 3.0 Cookbook.

There's a section on one-to-many mappings which I'm walking through but with my own database. It suggests I should do something like this to model a one to many relationship between customers and their domains:

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

Customer Mapping:

<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"/>
    <one-to-many class="Domain" />
  </list>
</class>

When I run this I get the following error:

XML validation error: The element 'list' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'one-to-many' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'index, list-index' in namespace 'urn:nhibernate-mapping-2.2'.

The book's example is a bit more complex in that they use table-per-subclass mappings:

alt text

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Eg.Core"
    namespace="Eg.Core">
  <subclass name="Movie" extends="Product">
    <property name="Director" />
    <list name="Actors" cascade="all-delete-orphan">
      <key column="MovieId" />
      <index column="ActorIndex" />
      <one-to-many class="ActorRole"/>  <-- Is this wrong?
    </list>
  </subclass>
</hibernate-mapping>

I'm guessing the book is wrong?

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

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

发布评论

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

评论(1

绝對不後悔。 2024-10-16 18:39:42

不,您的映射缺少 index 元素。 NHibernate 中的列表是有序集,如果您想要无序集,请使用包映射。

No, your mapping is missing the index element. A list in NHibernate is an ordered set, if you want an unordered set use bag mapping.

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