用于收集的 Nhibernate 自定义加载器

发布于 2024-09-10 10:07:54 字数 1209 浏览 10 评论 0原文

我真的希望有人可以帮助解决这个问题,已经尝试了一天半的各种组合......

基本上我有一些分层数据存储在单个表中,通常的parentID映射到行id场景。我在域对象中建模了一个属性,该属性返回给定项目的祖先列表。查看日志似乎一切都正常(即它检索并补充正确的行:

CollectionLoadContext - 4 collections were found in result set for role: Domain.Keyword.Ancestors
CollectionLoadContext - 4 collections initialized for role: Domain.Keyword.Ancestors

但是:集合实际上从未被填充,单步执行我的代码,Ilist 不包含它应该包含的内容 - 只有一个实例(与当前记录)!?日志中没有关于无法映射返回列的错误,它似乎没有正确填充?我确定我错过了一些明显的东西 - 但只是看不到它......

我的映射中有一个像这样的集合声明

<bag name="Ancestors" inverse="true"cascade="none" lazy="true" fetch="select" generic="true" >
  <key column="KeywordID"/>
  <one-to-many class="Domain.Keyword, BS.Core" />
  <loader query-ref="CustomAncestorLoader"  />
</bag>

...... 和一个名为 query 的自定义加载器,用于返回给定关键字的关键字祖先列表:

<sql-query name="CustomAncestorLoader">
<load-collection alias="Ancestors" role="Domain.Keyword.Ancestors"/>

SELECT  s.KeywordID, s.kwdhier, s.Keyword, s.Notes, s.position , s.ParentKeywordId
From dbo.utKeywordBranch(:ParentID) k join Keywords s on k.KeywordId = s.[KeywordID] </sql-query>

我已经在这方面花了很长时间了,所以我将不胜感激!

Im really hoping someone can help with this, have been trying various combinations for a day and a half now....

Basically I have a some hierarchical data stored in a single table, the usual parentID maps to a row id scenario. I have modelled a property within the domain object that returns a list of ancestors for a given item. It all seems to work looking at the logs (ie its retrieving and hydrating the correct rows :

CollectionLoadContext - 4 collections were found in result set for role: Domain.Keyword.Ancestors
CollectionLoadContext - 4 collections initialized for role: Domain.Keyword.Ancestors

HOWEVER: the collection never actually gets populated, stepping though my code the Ilist doesn't contain what it should - only a single instance (the same as the current record)!?? There are no errors in the logs about not being able to map the returned columns, it just doesn't appear to populate correctly? Im sure im missing something obvious - but just cant see it....

I have a collection declaration like this in my mapping

<bag name="Ancestors" inverse="true"cascade="none" lazy="true" fetch="select" generic="true" >
  <key column="KeywordID"/>
  <one-to-many class="Domain.Keyword, BS.Core" />
  <loader query-ref="CustomAncestorLoader"  />
</bag>

...
and a custom loader named query to return a list of keyword ancestors for a given keyword:

<sql-query name="CustomAncestorLoader">
<load-collection alias="Ancestors" role="Domain.Keyword.Ancestors"/>

SELECT  s.KeywordID, s.kwdhier, s.Keyword, s.Notes, s.position , s.ParentKeywordId
From dbo.utKeywordBranch(:ParentID) k join Keywords s on k.KeywordId = s.[KeywordID] </sql-query>

Im at the point where im tearing hair out as I spent so long on this already, so any help would be greatly appreciated!!

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

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

发布评论

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

评论(1

终陌 2024-09-17 10:07:54

好吧,当它SO明显时,你难道不讨厌它吗......

基本上集合被映射到一个名为 KeywordId 的键 - 这意味着它将根据该键上的当前对象 ID 加入,在返回的行中,没有一个对象具有此键(因为它们都是不同的对象)。

因此,为了解决这个问题,我在名为 OriginalID 的命名查询中添加了一个新的返回列,它只是返回我们作为参数传入的 ID。这样集合就有了可以映射到的东西:

<bag name="Ancestors" inverse="true" cascade="none" lazy="true"  fetch="select" generic="true" >
  <key column="OriginalID"/>
  <one-to-many class="Domain.SubjectKeyword, BS.Core" />
  <loader query-ref="CustomAncestorLoader"    />
</bag>

SELECT k.OriginalId, s.KeywordID, s.kwdhier, s.Keyword, s.Notes, s.position , s.ParentKeywordId
From dbo.uKeywordBranch(:Id) k join KATKeywords s on k.KeywordId = s.[KeywordID]

有时候,说话还是有帮助的!

Ok, don't you just hate it when its SO obvious.....

basically the collection was mapped to a key called KeywordId - meaning it will join based on the current object id on that key, in the rows returned none of the obects would have had this key (as they are all different objects).

So to fix I added a new return column in the named query called OriginalID, which simply returned the ID we pass in as a parameter. This way the collection has something to map to:

<bag name="Ancestors" inverse="true" cascade="none" lazy="true"  fetch="select" generic="true" >
  <key column="OriginalID"/>
  <one-to-many class="Domain.SubjectKeyword, BS.Core" />
  <loader query-ref="CustomAncestorLoader"    />
</bag>

SELECT k.OriginalId, s.KeywordID, s.kwdhier, s.Keyword, s.Notes, s.position , s.ParentKeywordId
From dbo.uKeywordBranch(:Id) k join KATKeywords s on k.KeywordId = s.[KeywordID]

Sometimes it just helps to talk it though!

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