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开发者_JS百科 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
精彩评论