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.