列表中的许多返回null项目

发布于 2025-01-26 16:02:33 字数 2368 浏览 4 评论 0原文

我在项目 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).

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2025-02-02 16:02:33

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文