views:

98

answers:

1

Yesterday I`ve asked a question Person -> Details database structure Now I need to make a NHibernate mapping, where the details are lazy loaded.

so my person map is:

<class name="Employee" table="Employee">
    <id name="Id" column="EmployeeId">
      <generator class="native" />
    </id>
    <property name="FName" column="FName"/>
    <property name="LName" column="LName"/>

    <one-to-one class="Contact" name="Contact"  property-ref="Employee" constrained="true" lazy="proxy"/>

  </class>

My Contact map:

  <class name="Contact" table="Contacts" lazy="true">
    <id name="Id" column="Id">
      <generator class="native"/>
    </id>
    <property name="PersonalMobilePhone" column="PersonalMobilePhone"/>
    <property name="HomeAdress"          column="HomeAdress"         />
    <property name="WorkTelephone"       column="WorkTelephone"      />
    <many-to-one name="Employee" class="Employee" column="EmployeeId" unique="true" />
  </class>

When I try to get concrete Employee from Repository, even if I never use my employee.Contact, profiler shows that there was a SELECT from Contacts table. How can I make this lazy?

A: 

Probably not possible, because NHibernate needs to do the inner join anyway to check if the contact exists. The performance difference between loading the contact immediately and lazy loading it would be unmeasurable. I don't understand why you want to use 2 tables for this or is it a legacy database?

Paco
Actualy I have much more fields in this table, and also I have another tables, like OfficeInfo, UserAccounts. And in my web UI i want to use Ajax Tabs to show all this stuff.
Alkersan
But it is an extra left join with and without lazy loading, so it will perform worse than using one table.
Paco
Ok. But what if I have a recursive reference from Contacts to Employee (like OfficeManager. - he is also an Employee, and he can also have an OfficeManager, etc. etc.). Then I is possibe to make a very long chain of joins. How to be here?
Alkersan
An extra table won't save you the joins. It will just be one more.
Paco
You can see how the queries are constructed in log4net. The log called NHibernate shows the queries that are executed by NHibernate.
Paco