2

我最近尝试开发一个“SessionWrapper”。它背后的想法是能够轻松地从有状态会话切换到无状态会话,而无需修改 DAO 和 XML 映射文件。

映射如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain" assembly=Domain">
  <class name="BatchScheduling" table="dbo.batch_scheduling">
    <id name="ID" column="batch_scheduling_id">
      <generator class="native" />
    </id>

    <property name="Name" column="batch_scheduling_name" not-null="true" />
    <property name="Description" column="batch_scheduling_description" not-null="true" />
    <property name="Type" column="batch_scheduling_type" not-null="true" />
    <property name="Cron" column="batch_scheduling_cron" not-null="true" />
    <property name="DateModified" column="batch_scheduling_date_modified" not-null="true" />

    <bag name="Parameters" cascade="all" table="dbo.batch_scheduling_parameters" lazy="true" inverse="true">
      <key column="batch_scheduling_id"/>
      <one-to-many class="BatchSchedulingParameter"/>
    </bag>

  </class>
</hibernate-mapping>

我知道无状态会话不支持延迟加载。我希望会话能够急切地获取映射中声明的所有相关对象/集合。

但是,当我尝试访问 BatchScheduling.Parameters 时,出现以下异常:

NHibernate.LazyInitializationException: Initializing[Domain.BatchScheduling#22]-failed to lazily initialize a collection of role: Domain.BatchScheduling.Parameters, no session or session was closed

任何的想法?

4

1 回答 1

2

我知道无状态会话不支持延迟加载。我希望会话能够急切地获取映射中声明的所有相关对象/集合。

这可能会导致整个数据库被加载到内存中,并且在大约 80-90% 的情况下会导致查询超出必要的数量,并且基本上会使无状态会话变得无用。

您可以使用的一种方法是急切地获取您需要的关系,使用类似 SetFetchMode

于 2013-05-15T08:02:51.137 回答