NHibernate:IUserType 的延迟加载 [英] NHibernate: Lazy loading of IUserType
问题描述
假设我们有一个存储客户详细信息的系统和一个存储员工详细信息的系统(假设情况!).当EmployeeSystem访问一个Employee时,Client信息是使用WCF从ClientSystem访问的,在IUserType中实现:
Say we have a system that stores details of clients, and a system that stores details of employees (hypothetical situation!). When the EmployeeSystem access an Employee, the Client information is accessed from the ClientSystem using WCF, implemented in an IUserType:
NHibernate 映射:
NHibernate mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="EmployeeSystem" namespace="EmployeeSystem.Entities">
<class name="Employee" table="`Employee`" >
<id name="Id" column="`Id`" type="long">
<generator class="native" />
</id>
<property name="Name"/>
<property
name="Client" column="`ClientId`"
lazy="true"
type="EmployeeSystem.UserTypes.ClientUserType, EmployeeSystem" />
</class>
</hibernate-mapping>
IUserType 实现:
IUserType implementation:
public class ClientUserType : IUserType
{
...
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
object obj = NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
IClientService clientService = new ClientServiceClient();
ClientDto clientDto = null;
if (null != obj)
{
clientDto = clientService.GetClientById(Convert.ToInt64(obj));
}
Client client = new Client
{
Id = clientDto.Id,
Name = clientDto.Name
};
return client;
}
...
}
即使我在属性上设置了 lazy="true",它也会在 Employee 加载后立即加载 Client.这是正确的行为吗?我必须在 NullSafeGet 中自己实现延迟加载还是我遗漏了什么?
Even though I have lazy="true" on the property, it loads the Client as soon as the Employee is loaded. Is this correct behaviour? Do I have to implement the lazy loading myself in NullSafeGet or am I missing something?
推荐答案
是否是一对一的关系.由于 hibernate 文档表明这一切都是可能的,xml 配置文件中甚至似乎有选项可以打开一对一关系的延迟加载,但它们显然没有效果.
Is it a one-to-one relation. As hibernate documentation suggests that this is all possible, there even appears to be options in the xml configuration file to switch on lazy loading for one-to-one relationships, but they have apparently no effect.
这篇关于NHibernate:IUserType 的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!