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).
I have checked the database for the Booker table and only have this item:
And for the table Item_booker I only have these:
This corresponds to my two following items :
Does anyone have any ideas?




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