views:

426

answers:

2

I have an Order mapping with many OrderItems. The mapping correctly saves both the Order and OrderItems to the database when I do a save on just the Order, but when I reload that order, the List of OrderItems that the order contains is filled with null values (for all the othe order items in the table) until it reaches the OrderItem that has the Order's foreign key. So the total count of null order items is the row count of the OrderItems table, (sans the actual order items that are related to the Order). Here's my mappings:

Order:

<list name="OrderItems" table="OrderItems" cascade="all" inverse="true" >
  <key column="OrderID"/>
  <index column="OrderItemID" />
  <one-to-many class="OrderItem" not-found="ignore" />
</list>

OrderItem:

<many-to-one name="Order" class="Order" column="OrderID" not-null="true" />

Here's the Order's class implementation:

public class Order : {
    private IList<OrderItem> orderItems = new List<OrderItem>();

I read that nHibernate doesn't support Lists as the many part of a relationship, but the save works correctly, just not the load. Do I have to convert everything over to Sets in order for this to work?

+1  A: 

Just of the top of my head I remember having problems with

<list>

and resorted to

<bag>

instead to map to the .NET IList interface. Not sure if that will solve your problem.

What may be helpful is to look at how the actual SQL that is hitting the database is different from what you expect by enabling logging to console.out (see this blog)

martijn_himself
Changed to bag and it worked great!!!
Josh
A: 

Isn't this caused because your OrderItems collection is lazy loaded ? If you specify lazy=false on your list (or bag) mapping, then this should not be the case anymore.

The question offcourse is, do you want to lazy load the OrderItems, or not ? :)

Frederik Gheysels