NHibernate 关于延迟加载的标准

发布于 2024-07-29 05:20:34 字数 288 浏览 2 评论 0原文

谁能告诉我是否可以向 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 技术交流群。

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

发布评论

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

评论(1

撩发小公举 2024-08-05 05:20:34

实现这一点的最佳方法是使用过滤器。 在映射(实体和包)中定义过滤器,然后在选择“部门”之前使用所需的日期参数启用过滤器。

session.EnableFilter("HireDateFilter").SetParameter("dateParameter", DateTime.Now.AddDays(-30));

您还可以按原样保留映射,并在延迟加载的集合上应用自定义的过滤器。

session.CreateFilter(department.Employees, "HireDate >= :dateParameter").SetParameter("dateParameter", DateTime.Now.AddDays(-30));

如果您唯一想做的就是应用限制,我更喜欢映射过滤器选项,因为这样您就可以有选择地急切获取集合,从而在单个选择中获得对象图。

如果您想要执行其他操作(例如分页),则 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.

session.EnableFilter("HireDateFilter").SetParameter("dateParameter", DateTime.Now.AddDays(-30));

You could also leave the mapping as-is and apply a custom defined filter on the lazy loaded collection.

session.CreateFilter(department.Employees, "HireDate >= :dateParameter").SetParameter("dateParameter", DateTime.Now.AddDays(-30));

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.

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