Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate在运行时更改获取模式_Hibernate_Mapping - Fatal编程技术网

Hibernate在运行时更改获取模式

Hibernate在运行时更改获取模式,hibernate,mapping,Hibernate,Mapping,我使用一对多关系将两个表关联在一起: 员工->部门:通过员工表中的部门id建立关系 我使用hibernate:并且我的hibernate映射文件是: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibe

我使用一对多关系将两个表关联在一起: 员工->部门:通过员工表中的部门id建立关系

我使用hibernate:并且我的hibernate映射文件是:

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

我的问题是:有时我需要让员工了解他的部门,而有时我只需要员工信息而不需要部门信息。。。。。部门和员工也是如此。。。。使用hibernate上面的映射文件将部门及其用户带到我是否需要员工的位置。。。如何定义我的休眠需求以获取我所需的


谢谢

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

  • 获取数据的常用简单查询(“lazy”)。例如,“从员工E中选择E,其中…”

  • 相同的查询使用“fetchjoin”强制Hibernate获取“childs”。例如,“从员工左侧选择E加入提取E部门,其中…”


LLP,Andrea

您可以使用ICriteria来接您的员工

您可以使用ICriteria的SetFetchMode方法确定是否应提取部门:

这将确保不提取部门:

ICriteria crit = theSession.CreateCriteria (typeof(Employee));
crit.SetFetchMode ("Department", FetchMode.Lazy)
使用此代码,将获取部门

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

有人说,最好在映射中使用默认的fetchmode(我想这是惰性的),并在每个特定场景中指定fetchmode。(也就是说,在您的存储库中)。

一种方法是使用两个类来表示员工:

  • 员工
    ,其部门信息已通过映射
  • EmployeeSummary
    ,其中仅包含员工数据本身
然后,这两个类对
employee
表都有独立的绑定,但只有
employee
还定义了与
department
的关系

然后,当您需要加载
Employee
实例的所有信息时,以及当您只需要加载员工信息时,您可以加载
EmployeeSummary
实例


您可以通过在两个employee类中引入像
AbstractEmployee
这样的公共超类来删除ORM绑定和业务逻辑的任何重复
. 再看看这个例子:

在这种情况下,我认为这不是一个好主意。如果您的类包含复杂的BL呢?你必须复制它吗?如果你引入了公共超类,不一定,但很好的一点-我已经更新了答案来反映这一点。如果在单个会话中,一个雇员和一个雇员摘要都从同一个数据库记录加载,然后其中一个或两个都被更改了?无论如何都可能发生这种情况-如果两个员工记录实例被独立地加载和更改会怎么样?希望您的Hibernate版本号字段能以任何一种方式为您节省时间…它对我不起作用。它仍然获取关联的实体,但不使用join子句。相反,它调用另一个select语句。