2009-04-28 133 views
1

我有一個關係數據庫,包含表格和各種關係(1> N,N> 1,1> 1和N> N)..休眠模式,關係

讓我們這些表是一個「Department」表中,這個表是我DB中最複雜的表,因爲它與DB中大多數表有關係。

的XML映射文件「Department.hbm.xml」的模樣:

<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> 

有些時候我需要沒有任何關係,以獲取部門。其他時間,我需要一些它的關係來獲取部門...

什麼是做到這一點的最好辦法,花一在性能和DB的命中數的考慮。

回答

1

通過設置lazy啓用延遲加載= 「true」 是最好的辦法。你有沒有理由在整個映射過程中將其設置爲false?

另一種方法是在你的對象模型中的所有關係不是模型。

+0

感謝您的幫助... 但是,如果我爲任何關係設置lazy =「true」,則在我需要它時將不可用,例如:讓我們來看看Department和ApplicationUser之間的關係,一個和它的名字=「applicationUserByManagerId」,如果我將其設置爲lazy =「真」,所以我-in當代碼 - 我調用:department.getApplicationUserByManagerId();它會拋出一個錯誤。要避免這個錯誤我設置爲lazy =「假」,並開始面臨的第一個問題是:「把所有的關係,雖然我並不需要它」,所以如何在這種情況下做的。請諮詢我... 請多關照......賽義德 – Saeed 2009-04-29 06:31:22