tags:

views:

312

answers:

1

Hello everybody...

I have a relational DB, contains tables and all kinds of relations(1>n, n>1, 1>1 and n>n)..

Let's take one of these tables which is "Department" table, this table is the most complicated table in my DB, because it has relations with most of the table in the DB.

The XML mapping file "Department.hbm.xml" looks like:

<hibernate-mapping>
 <class catalog="MOIDB"
  name="com.ebla.moi.correspondence.model.entity.db.Department"
  schema="dbo" table="Department">
  <id name="id" type="java.lang.Integer">
   <column name="Id"/>
   <generator class="increment"/>
  </id>
  <many-to-one
   class="com.ebla.moi.correspondence.model.entity.db.Department"
   fetch="select" name="department">
   <column name="Parent"/>
  </many-to-one>
  <many-to-one
   class="com.ebla.moi.correspondence.model.entity.db.ApplicationUser"
   fetch="join" lazy="false" name="applicationUserByManagerId">
   <column name="Manager_Id"/>
  </many-to-one>
  <many-to-one
   class="com.ebla.moi.correspondence.model.entity.db.ApplicationUser"
   fetch="join" lazy="false" name="applicationUserByAssistantId">
   <column name="Assistant_Id"/>
  </many-to-one>
  <property generated="never" lazy="false" name="description" type="java.lang.String">
   <column length="80" name="Description" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="type" type="java.lang.Integer">
   <column name="Type" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="prefix" type="java.lang.String">
   <column length="20" name="Prefix" unique="true"/>
  </property>
  <property generated="never" lazy="false" name="serialPrefix" type="java.lang.String">
   <column length="20" name="Serial_Prefix"/>
  </property>
  <property generated="never" lazy="false" name="telephoneNumbers" type="java.lang.String">
   <column length="100" name="Telephone_Numbers"/>
  </property>
  <property generated="never" lazy="false" name="faxNumbers" type="java.lang.String">
   <column length="100" name="Fax_Numbers"/>
  </property>
  <property generated="never" lazy="false" name="smsMaxTime" type="java.lang.Integer">
   <column default="30" name="SMS_Max_Time"/>
  </property>
  <property generated="never" lazy="false" name="emailMaxTime" type="java.lang.Integer">
   <column default="30" name="Email_Max_Time"/>
  </property>
  <property generated="never" lazy="false" name="hasCorrespondence" type="java.lang.Boolean">
   <column name="Has_Correspondence" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="email" type="java.lang.String">
   <column length="50" name="Email"/>
  </property>
  <property generated="never" lazy="false" name="logoImageName" type="java.lang.String">
   <column length="50" name="Logo_Image_Name"/>
  </property>
  <set inverse="true" name="departmentDocumentTypeSerials" sort="unsorted">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.DepartmentDocumentTypeSerial"/>
  </set>
  <set inverse="true" name="departmentGlobalVariableses" sort="unsorted">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.DepartmentGlobalVariables"/>
  </set>
  <set inverse="true" name="departmentFiles" sort="unsorted">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.DepartmentFile"/>
  </set>
  <set catalog="MOIDB" name="applicationUsers" schema="dbo"
   sort="unsorted" table="Application_User_Department">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <many-to-many class=""
    entity-name="com.ebla.moi.correspondence.model.entity.db.ApplicationUser" unique="false">
    <column name="Application_User_Id" not-null="true"/>
   </many-to-many>
  </set>
  <set inverse="true" name="departments" sort="unsorted">
   <key>
    <column name="Parent"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.Department"/>
  </set>
  <set inverse="true" lazy="false" name="departmentClassifications" sort="unsorted">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.DepartmentClassification"/>
  </set>
  <set inverse="true" lazy="false" name="depCorrespondenceSites" sort="unsorted">
   <key>
    <column name="Department_Id" not-null="true"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.db.DepCorrespondenceSite"/>
  </set>
 </class>
</hibernate-mapping>

Some times i need to fetch the department without any relations. Other time, i need to fetch departments with some of it's relations...

What is the best way to do that.. take in the consideration the performance and the number of DB hits.

+1  A: 

Enabling lazy loading by setting lazy="true" is the best way. Is there a reason you have set it to false throughout your mapping?

Another approach is to not model all the relations in your object model.

Jamie Ide
Thanks for your help...But if i set lazy="true" for any relations, it will be unavailable when i need it, fro example : let's take the relation between Department and ApplicationUser which is many-to-one and its name="applicationUserByManagerId", if i set it to lazy="true", so when -in my code- i invoke : department.getApplicationUserByManagerId(); it will throw an error.. to avoid this error i set lazy="false", and start facing the first problem which is : "bring all the relation although i don't need it", so what to do in this case. PLEASE Advice me...Thanking you in ADVANCE... Saeed
Saeed
Lazy loading isn't working for you because the ISession used to retrieve Department is closed when you're attempting to access the child collections. It's possible to re-associate a detached object with a new ISession using ISession.Lock. See http://intellect.dk/post/Detached-objects-in-nHibernate-and-Lazy-loading.aspx.I suggest removing all the collections except one from Department and working with that simple case while you learn how best to implement lazy-loading in your app.
Jamie Ide