Hibernate与关系

时间:2009-04-28 13:07:04

标签: hibernate

我有一个关系数据库,包含表格和各种关系(1> n,n> 1,1> 1和n> n)..

让我们从其中一个表中选择“Department”表,这个表是我数据库中最复杂的表,因为它与数据库中的大部分表有关系。

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>

有些时候我需要在没有任何关系的情况下取得部门。其他时候,我需要获取一些部门的关系......

这样做的最佳方式是......考虑性能和数据库命中次数。

1 个答案:

答案 0 :(得分:1)

通过设置lazy =“true”启用延迟加载是最好的方法。您是否有理由在整个映射过程中将其设置为false?

另一种方法是不对对象模型中的所有关系建模。