我最近尝试开发一个“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
任何的想法?