如果表中没有索引,如何配置 NHibernate 映射到数组?

2024-01-05

我有一个现有的 POCO 类库,其中子集合全部存储在数组中。例如,Customer 类有一个 Invoice[] 数组来保存其发票:

class Customer
{
    public int ID;
    public Invoice[] _invoices;
}

class Invoice
{
    public int ID;
    public int CustomerID;
    public string SomeData;
}

我无法更改这些类,并且我想使用 NHibernate 将它们映射到现有数据库。

我看了看<array>映射,但似乎需要一个<index>元素。在我的数据库中,[Invoice]表没有索引列。加载客户发票时,我不希望它们位于数组中的任何特定位置。

我有什么选择?


不幸的是,你的选择是改变你的班级或你的桌子。

A)你可以改变Customer将发票申报为IList而不是数组;然后你将能够将它们映射为未分类的袋子 https://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/collections.html#collections-mapping。您甚至可以将其按某些列排序:

<bag name="Invoices" table="Invoices" order-by="ID ASC"> <!-- order-by is optional -->
  <key column="CustomerID"/>
  <element column="SomeData" type="String"/>
  <!-- or use one-to-many if Invoice is mapped as entity -->
  <one-to-many class="Whatever.Invoice, Whatever"/>
</bag>

B)。您可以更改表以包含索引列并将发票映射为真实发票<array>:

<array name="Invoices" table="Invoices" order-by="ID ASC"> <!-- order-by is optional -->
  <key column="CustomerID"/>
  <index column="IndexNr"/>
  <element column="SomeData" type="String"/>
</array>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果表中没有索引,如何配置 NHibernate 映射到数组? 的相关文章

随机推荐