NHibernate 非唯一子集合急切加载(一对多或多对多)
当我使用一对多关系时,我不断得到:
<块引用>集合不与任何会话关联
急切加载时出错,如果延迟加载则不会获取任何子项。
当使用多对多时,我得到不必要的和错误的连接。
数据方案是这样的:
项目:
- 整数 ID (PK)
- 字符串名称
- int StorageId(FK 存储,非唯一用途)
)
存储(映射中忽略):
- 整数 ID (PK)
- 字符串名称
TransporterToStorage(包含信息,不仅仅是链接表):
- 整数 ID (PK)
- int StorageId(FK 存储)
- int TransporterId(FK Transporter)
- 字符串 TransportLineName
传输器(在映射中被忽略):
- 整数 ID (PK)
- 字符串名称
Item 应由 C# / NHibernate 类表示,带有附加集合属性 TransportersToStorage
,包含 0-n 个条目。由于性能原因,我只想将 Item.StorageId
映射到 TransporterToStorage.StorageId
并忽略 Storage
表,该表保存主表两者的关键。
<bag name="TransportersToStorages" ... >
<key property-ref="StorageId" unique="false" ... />
<one-to-many class="TransporterToStorage" column="StorageId" ... />
</bag>
一对多似乎违反了一些 NHibernate 规则,因为相同的 TransporterToStorage
条目可以属于多个 Item。这可能是以下原因:
集合不与任何会话关联
。
具体的查询就像(快速伪 HQL,但实际上是用 Criteria 完成的):
select Item inner join fetch TransporterToStorage tts
where tts.TransporterId = :p1 and tts.StorageId in (:p2, :p3, :p4)
能否仅通过发送到数据库的 SQL 中的一个连接来完成多对多或类似的关联?
最好用什么来映射这个?
默认情况下,子集合应延迟加载(意思是:根本不加载),而在给定情况下则应急切加载。
when I use one-to-many relations, I keep getting:
collection is not associated with any session
errors when loading eagerly, and not getting any children if I load lazily.
when using many-to-many, I get unnecessary and wrong joins.
The data scheme is like this:
Item:
- int ID (PK)
- string Name
- int StorageId (FK Storage, non-unique usage)
Storage (ignored in mapping):
- int ID (PK)
- string Name
TransporterToStorage (contains info, not only link table):
- int ID (PK)
- int StorageId (FK Storage)
- int TransporterId (FK Transporter)
- string TransportLineName
Transporter (ignored in mapping):
- int ID (PK)
- string Name
Item shall be represented by a C# / NHibernate class, with an additional collection property TransportersToStorage
, containing 0-n entries. Due to performance reasons, I want to map only the Item.StorageId
to the TransporterToStorage.StorageId
and ignore the Storage
table, which holds the primary key for both.
<bag name="TransportersToStorages" ... >
<key property-ref="StorageId" unique="false" ... />
<one-to-many class="TransporterToStorage" column="StorageId" ... />
</bag>
one-to-many seems to violate some NHibernate rules, because equal TransporterToStorage
entries can belong to multiple Items. This is probably the cause of the:
collection is not associated with any session
error on eager loading.
The specific query is like (quick pseudo HQL, but actually done with Criteria):
select Item inner join fetch TransporterToStorage tts
where tts.TransporterId = :p1 and tts.StorageId in (:p2, :p3, :p4)
Can many-to-many or a similar association be done with only one join in the SQL sent to the database?
What is best to map this?
The child collection shall be loaded lazily by default (meaning: not at all), and eagerly in the given case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这里存在会话管理问题,我怀疑您的会话在您访问惰性/急切加载的子级之前已关闭。您可能需要更改管理会话的方式或将父/子加载到 DTO 中并从您的方法返回此值。
如果不了解更多关于处理会话的方式,就很难给出更明确的答案。
I suspect a session management problem here, I suspect your session is closed BEFORE you access your lazy/eager loaded children. You might need to change the way you manage your sessions or load parent/children into a DTO and return this from your method.
Without knowing more about the way you are handling sessions a more definitive answer is difficult.