6

我有两个使用一对多关系关联在一起的表:员工->部门:以及通过员工表中的部门ID的关系。

我使用休眠:我的休眠映射文件是:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping default-lazy="false">
 <class catalog="moi"
  name="com.ebla.moi.correspondence.model.entity.user.User" table="user">
  <id name="id" type="java.lang.Long">
   <column name="id"/>
   <generator class="identity"/>
  </id>
  <many-to-one
   class="com.ebla.moi.correspondence.model.entity.department.Department"
   fetch="select" name="department">
   <column name="department_id"/>
  </many-to-one>
  <property generated="never" lazy="false" name="name" type="java.lang.String">
   <column length="128" name="name" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="email" type="java.lang.String">
   <column length="128" name="email" not-null="true" unique="true"/>
  </property>
  <property generated="never" lazy="false" name="maritalStatus" type="java.lang.Short">
   <column name="marital_status" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="hireDate" type="java.lang.String">
   <column length="64" name="hire_date"/>
  </property>
 </class>
</hibernate-mapping>

第二个映射文件是:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping default-lazy="false">
 <class catalog="moi"
  name="com.ebla.moi.correspondence.model.entity.department.Department" table="department">
  <id name="id" type="java.lang.Long">
   <column name="id"/>
   <generator class="identity"/>
  </id>
  <property generated="never" lazy="false" name="name" type="java.lang.String">
   <column length="256" name="name" unique="true"/>
  </property>
  <set inverse="true" name="users" sort="unsorted">
   <key>
    <column name="department_id"/>
   </key>
   <one-to-many class="com.ebla.moi.correspondence.model.entity.user.User"/>
  </set>
 </class>
</hibernate-mapping>

我的问题是:有时我需要获取员工和他的部门,而其他时候我只需要没有部门信息的员工信息.....同样的事情与部门和员工....使用上面的映射文件如果我需要员工,休眠会带来部门及其用户......如何定义我的休眠需求以获取我需要的东西......

谢谢你

4

4 回答 4

10

您可以将关系映射为“惰性”并编写两个查询来获取数据:

  • 获取数据的常用简单查询(“惰性”)。例如“从员工 e 中选择 e ...”

  • 使用“fetch join”来强制 Hibernate 获取“childs”的相同查询。例如“select e from Employee left join fetch e.department where ...”

律师事务所,安德里亚

于 2009-02-02T11:44:35.523 回答
8

You can use ICriteria to fetch your Employee.

You can use the SetFetchMode method of the ICriteria to determine whether the Department should be fetched, or not:

This will make sure that the Department is not fetched:

ICriteria crit = theSession.CreateCriteria (typeof(Employee));
crit.SetFetchMode ("Department", FetchMode.Lazy)

With this code, the department will be fetched.

ICriteria crit = theSession.CreateCriteria (typeof(Employee));
crit.SetFetchMode ("Department", FetchMode.Join)

Some say that it is best-practice to use the default fetchmode in the mappings (which would be lazy, i guess), and specify the fetch-mode in every specific scenario. (That is, in your repositories).

于 2009-02-02T11:52:04.963 回答
2

One way of doing this is to have two classes representing an employee:

  • Employee which has department information mapped through;
  • EmployeeSummary which only has the employee data itself.

Both classes then have independent bindings onto the employee table, but only Employee also has the relationship onto department defined.

Then when you need all the information you load Employee instances, and when you just need employee information you load EmployeeSummary instances.

You can remove any duplication of ORM bindings and business logic by introducing a common superclass like AbstractEmployee to both of the employee classes.

于 2009-02-02T12:21:29.053 回答
1

我认为您正在寻找的是fetch profiles 。也看看这个例子:http ://arjan-tijms.omnifaces.org/2012/04/fetching-arbitrary-object-graphs-in-jpa.html

于 2012-10-26T05:41:15.613 回答