用于收集的 Nhibernate 自定义加载器
我真的希望有人可以帮助解决这个问题,已经尝试了一天半的各种组合......
基本上我有一些分层数据存储在单个表中,通常的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,当它SO明显时,你难道不讨厌它吗......
基本上集合被映射到一个名为 KeywordId 的键 - 这意味着它将根据该键上的当前对象 ID 加入,在返回的行中,没有一个对象具有此键(因为它们都是不同的对象)。
因此,为了解决这个问题,我在名为 OriginalID 的命名查询中添加了一个新的返回列,它只是返回我们作为参数传入的 ID。这样集合就有了可以映射到的东西:
有时候,说话还是有帮助的!
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:
Sometimes it just helps to talk it though!