2009-04-28 102 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