1

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?

Paul T Davies
  • 2,527
  • 2
  • 22
  • 39

1 Answers1

0

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

sushant jain
  • 213
  • 1
  • 4
  • 12
  • Well it's a bit daft, but I can't complain too much about an otherwise excellent tool that is, after all, free. I might look into a way of returning a proxy from NullsafeGet if the Id of the client is accessed, and fetching the entity if any other fields are required. For the moment, I've found the the Client can be lazy loaded if I Load() the Employee rather than Get()ing it. – Paul T Davies Sep 03 '11 at 12:38
  • -1. First of all one-to-one can be lazy: http://community.jboss.org/wiki/Someexplanationsonlazyloadingone-to-one. Secondly this will not solve OP problem because he needs a custom loading (IUserType that loads object from WCF). – Dmitry Sep 05 '11 at 01:59