NHibernate:延迟加载IUserType [英] NHibernate: Lazy loading of IUserType

查看:83
本文介绍了NHibernate:延迟加载IUserType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个存储客户细节的系统和一个存储雇员细节的系统(假设情况!)。当EmployeeSystem访问一个Employee时,客户端信息可以通过使用WCF的ClientSystem访问,实现在IUserType中:



 < hibernate-mapping xmlns =urn:nhibernate-mapping-2.2
assembly =EmployeeSystemnamespace =EmployeeSystem.Entities>
< class name =Employeetable =`Employee`>
< id name =Idcolumn =`Id`type =long>
< generator class =native/>
< / id>
< property name =Name/>
< property
name =Clientcolumn =`ClientId`
lazy =true
type =EmployeeSystem.UserTypes.ClientUserType,EmployeeSystem/>
< / class>
< / hibernate-mapping>

IUserType实现:

  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));
}

客户端客户端=新客户端
{
Id = clientDto.Id,
名称= clientDto.Name
};

返回客户端;
}

...

}

即使我在属性上有lazy =true,只要加载Employee,它就会加载客户端。这是正确的行为?我是否必须在NullSafeGet中自己实现惰性加载,或者我是否缺少某些内容?

解决方案

它是一对一关系。由于hibernate文档表明这是完全可能的,甚至似乎在xml配置文件中有一些选项可用于切换一对一关系的延迟加载,但它们显然不起作用。



一对一懒惰关联


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 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 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;
    }

    ...

}

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?

解决方案

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.

one-to-one lazy association

这篇关于NHibernate:延迟加载IUserType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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