Lazy load nhibernate one-to-one

2019-06-03 07:47发布

问题:

I have a true one to one mapping. But I would like to use lazy loading(load on demand).

I have Class Person with a association with Class Address. The mapping looks like this..

PERSON

<one-to-one name="address" class="Person" cascade="all-delete-orphan" access="field">

ADDRESS

<class name="Address" table="Address" lazy="true">
<id name="id" column="addressId" type="Int32" access="field">
  <generator class="foreign">
    <param name="property">person</param>
  </generator>      
</id>
<one-to-one name="person" class="Address" constrained="true" access="field" />

Does anyone see something wrong with this? How do I enable proxy/lazy loading for address?

Thanks

回答1:

Heres a good discussion on the topic http://ayende.com/Blog/archive/2007/05/10/NHibernate-onetoone.aspx

Crucial quote: "In other words, one-to-one cannot be lazily loaded, which is one of the reasons why it is recommended to use two many-to-one instead."

Also see https://www.hibernate.org/162.html and NHibernate: how to enable lazy loading on one-to-one mapping