列表中的许多返回null项目
我在项目和 Booker 之间有许多与众不同的关系。 这是一个表的示例:
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)
);
相应的模型如下:
public class Item : AbstractPersistentEntity
{
public virtual string Name { get; set; }
public virtual IList<Booker> Bookers { get; set; }
}
我使用此映射:
<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>
我的问题是,当我从数据库中检索数据时,我的项目比 Booker list中的项目多于预期。而且额外的项目为null(数据库中不存在)。
我已经检查了Booker表的数据库,只有此项目:
< img src =“ https://i.sstatic.net/gn0kz.png” alt =“在此处”>
,对于table item_booker,我只有这些:
i.sstatic.net/70mzj.png“ rel =“ nofollow noreferrer 对应于我以下两个项目:
有人有任何想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
&lt; list-index列=“ id”/&gt;
告诉NHIBERNATE将ID视为索引。由于它从1开始,它将在索引1插入记录,因此必须在索引0处有空。
<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"