NHibernate 关于延迟加载的标准
谁能告诉我是否可以向 NHibernate 延迟加载集合添加条件。
例如,假设我有一个包含员工的部门...我通过 id 获取该部门,然后延迟加载员工...但是假设有 1000 名员工,我只想要最近雇用的员工30天。
获取部门 ID(1)
|
--延迟加载员工(其中 HireDate >= 7/1/2009)
似乎我可以使用映射文件中的过滤器来执行此操作...但是我可以向延迟加载某种条件吗加载代码?
Can anyone tell me if it's possible to add criteria to an NHibernate lazy loaded collection.
For example let's say I have a department that contains employees... I get the department by id and then I lazy load the employees... however lets say there are 1000's of employees and I only want the employees that were hired in the last 30 days.
GetDeptById(1)
|
--Lazy load employees (where HireDate >= 7/1/2009)
Seems like I could possibly do this with filters in the mapping file... but can I add some sort of criteria to the lazy load in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现这一点的最佳方法是使用过滤器。 在映射(实体和包)中定义过滤器,然后在选择“部门”之前使用所需的日期参数启用过滤器。
您还可以按原样保留映射,并在延迟加载的集合上应用自定义的过滤器。
如果您唯一想做的就是应用限制,我更喜欢映射过滤器选项,因为这样您就可以有选择地急切获取集合,从而在单个选择中获得对象图。
如果您想要执行其他操作(例如分页),则 CreateFilter 会更好,但对于更复杂的场景,针对员工的查询会更好。
The best way to implement this would be with a filter. Define the filter in the mappings (both entity and the bag) and then before selecting the "department" enable the filter with parameter the date you want.
You could also leave the mapping as-is and apply a custom defined filter on the lazy loaded collection.
If the only thing you want to do is apply the restriction, i would prefer the mapping-filter option because that way you can selectively eager-fetch the collection, having this way the object graph in a single select.
If you want to do additional operations (like paging) the CreateFilter would be better although for more complex scenarios a query targeting the Employees would be better imo.