nhibernate-为一对多关系禁用自动\延迟加载子记录 [英] nhibernate - disable automatic\lazy loading of child records for one to many relationsihps

查看:79
本文介绍了nhibernate-为一对多关系禁用自动\延迟加载子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以禁止在nHibernate中自动加载子记录(对于一个:很多关系).

I would like to know if there is a way to disable automatic loading of child records in nHibernate ( for one:many relationships ).

我们可以轻松地关闭属性的延迟加载,但是我想要的是禁用任何类型的自动加载(延迟和非延迟两者).我只想通过查询(即HQL或Criteria)加载数据

We can easily switch off lazy loading on properties but what I want is to disable any kind of automatic loading ( lazy and non lazy both ). I only want to load data via query ( i.e. HQL or Criteria )

我仍然想在映射文件中定义父子记录之间的关系,以促进HQL并能够加入父子实体,但是我不希望将子记录作为父记录的一部分加载,除非查询父记录 明确指出(通过热切获取等).

I would still like to define the relationship between parent child records in the mapping file to facilitate HQL and be able to join parent child entities, but I do not want the child records to be loaded as part of the parent record unless a query on the parent record explicitly states that ( via eager fetch, etc ).

示例: 从数据库中获取部门记录不应从数据库中获取所有员工记录,因为可能永远不需要它.

Example: Fetching Department record from the database should not fetch all employee records from the database because it may never be needed.

这里的一个选项是将Department上的Employees集合设置为延迟加载.这种方法的问题在于,一旦将对象提供给调用API,它就可以触摸"延迟加载属性,并且将从数据库中获取整个列表.

One option here is to set the Employees collection on Department as lazy load. The problem with this approach is that once the object is given to the calling API it can 'touch' the lazy load property and that will fetch the entire list from the db.

我试图使用'evict'-断开对象的连接,但它似乎并非一直都在起作用,并且未对对象进行任何深度的逐出. 另外,它使用代理类抽象了延迟加载的属性类型,该代理类稍后在代码中会造成严重破坏,我们尝试通过反射对对象进行操作,并且在对象上遇到未扩展的类型.

I tried to use 'evict' - to disconnect the object but it does not seem to be working at all times and does not do a deep evict on the object. Plus it abstracts the lazy loaded property type with a proxy class that plays havoc later in the code where we are trying to operate on the object via reflection and it encounters unexpended type on the object.

我是nHibernate的初学者,任何指针或帮助将大有帮助.

I am a beginner to nHibernate, any pointers or help would be of great help.

推荐答案

鉴于您的请求,您不能简单地从Department映射到Employees,也不能在您的Department上拥有Employees属性.这意味着您总是必须命中数据库才能找到数据库的雇员.

Given your request, you could simply not map from Department to Employees, nor have an Employees property on your department. This would mean you always have to make a database hit to find the employees of a database.

很抱歉,如果这些代码示例无法立即使用,我目前不在编译器附近

因此,您的部门课程可能类似于:

So, your department class might look like:

 public class Department 
 { 
     public int Id { get; protected set; }
     public string Name { get; set; }
     /* Equality and GetHashCode here */
 }

您的员工看起来像:

 public class Employee
 { 
     public int Id { get; protected set; }
     public Name Name { get; set; }
     public Department Department { get; set; }
     /* Equality and GetHashCode here */
 }

每当您想为某个部门找到员工时,您都必须致电:

Any time you wanted to find Employees for a department, you've have to call:

/*...*/
session.CreateCriteria(typeof(Employee))
    .Add(Restrictions.Eq("Department", department)
    .List<Employee>();

仅仅因为您的规范中显示部门有很多员工",并不意味着您必须将其映射为双向关联.如果您可以保持关联的单向性,那么实际上也可以使数据访问也变得顺畅.

Simply because your spec says "Departments have many Employees", doesn't mean you have to map it as a bi-directional association. If you can keep your associated uni-directional, you can really get your data-access to fly too.

Google 域驱动设计"聚合,或参阅Eric Evan关于域驱动设计的书的第125页了解更多信息

Google "Domain Driven Design" Aggregate, or see Page 125 of Eric Evan's book on Domain Driven Design for more information

这篇关于nhibernate-为一对多关系禁用自动\延迟加载子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆