NHibernate:延迟加载IUserType [英] NHibernate: Lazy loading of 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.
这篇关于NHibernate:延迟加载IUserType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!