NHibernate many to many return null item in a list

58 Views Asked by At

I have a many to many relationship between Item and Booker. Here is an example of a table :

create table Item (
    id int generated by default as identity primary key,
    name varchar(200) not null
);

CREATE TABLE Booker (
    id int generated by default as identity primary key,
    fisrtName varchar(200),
    lastName varchar(200)
);

create table Item_Booker (
    id int generated by default as identity primary key,
    itemId int not null references Item(id),
    bookerId int not null references Booker(id)
);

the corresponding model is as follows :

    public class Item : AbstractPersistentEntity
    {
        public virtual string Name { get; set; }
        public virtual IList<Booker> Bookers { get; set; }
    }

And I use this mapping :

<hibernate-mapping
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:nhibernate-mapping-2.2" xsi:schemaLocation="urn:nhibernate-mapping-2.2 ../../nhibernate-mapping.xsd"
    namespace="BirthList.Core.Model" assembly="BirthList.Core">


    <class name="Item" table="item">
        <id name="Id" column="id">
            <generator class="identity"/>
        </id>
        <property name="Name" column="name"/>
        <!-- Association n-n -->
        <list name="Bookers" table="item_booker">
            <key column="itemId"/>
            <list-index column="id"/>
            <many-to-many class="Booker" column="bookerId"/>
        </list>
    </class>
</hibernate-mapping>

My problem is that when I retrieve my data from the database I have more items than expected in my Booker list. And the extra items are null (and are not present in the database).

enter image description here

I have checked the database for the Booker table and only have this item:

enter image description here

And for the table Item_booker I only have these:

enter image description here

This corresponds to my two following items :

enter image description here

Does anyone have any ideas?

1

There are 1 best solutions below

0
Firo On

<list-index column="id"/>

tells NHibernate to treat the id as an index. since it starts at 1 it will insert the record at index 1 so there must be null at index 0. what you probably want is orderby="id"