NHibernate: Lazy loading of IUserType

▼魔方 西西 提交于 2019-11-28 11:26:29

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7286132/nhibernate-lazy-loading-of-iusertype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!