How to configure NHibernate to map to an array if there is no index in the table?

I have an existing POCO class library where all collections are stored in arrays. For example, the Customer class has an Invoice [] array for storing its invoices:

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

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

I cannot change these classes, and I want to map them to an existing database using NHibernate.

I looked at the display <array>, but it looks like it needs an element <index>. In my database, the table [Invoice]does not have an index column. When Customer invoices are loaded, I do not expect them to be at any particular position in the array.

What are my options?

+3
source share
1 answer

, , .

A) Customer, - IList ; . :

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

). , <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>
+2

Source: https://habr.com/ru/post/1723063/


All Articles