【问题标题】:collection is not associated with any session + Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection集合不与任何会话相关联+引起:org.hibernate.LazyInitializationException:非法访问加载集合
【发布时间】:2012-02-08 00:40:58
【问题描述】:

hbm 文件:

<class name="com.shaklee.productCatalogue.persistence.CategoryMasterDBVO" table="PRD_CATEGORY_MASTER" lazy="false">
    <meta attribute="implement-equals" inherit="false">true</meta>
    <meta attribute="extra-import" inherit="false">java.math.BigDecimal</meta>

    <cache usage="nonstrict-read-write"/>

    <!-- Unique identifier -->
    <id name="categoryGuid" column="category_guid" type="java.lang.Long">
        <meta attribute="scope-set">protected</meta>
        <generator class="com.quinstreet.persistence.GuidGenerator"/>
    </id>

    <!--  self reference -->
    <many-to-one name="parent" column="CATEGORY_PARENT_GUID"
        class="com.shaklee.productCatalogue.persistence.CategoryMasterDBVO" />

    <set name="children" cascade="none" inverse="false" lazy="false">
        <key column="CATEGORY_PARENT_GUID"/>
        <one-to-many class="com.shaklee.productCatalogue.persistence.CategoryMasterDBVO"/>
    </set>

    <set name="assignedProducts" table="PRD_PRODUCT_CATEGORY" lazy="false" batch-size="25" cascade="none">
        <key column="CATEGORY_GUID" />
        <many-to-many column="PRODUCT_GUID"
            class="com.quinstreet.persistence.product.ProductDBVO" />
    </set>

    <idbag name="details" table="PRD_CATEGORY_MASTER_L10N" lazy="true"
        batch-size="25">
        <cache usage="nonstrict-read-write"/>

        <collection-id type="long" column="guid">
            <meta attribute="scope-set">protected</meta>
            <generator class="com.quinstreet.persistence.GuidGenerator"></generator>
        </collection-id>

        <key column="CATEGORYGUID" foreign-key="FK_PRD_CATEGORY_MASTER_L10N_CATEGORY" />

        <composite-element
            class="com.shaklee.productCatalogue.persistence.CategoryMasterl10nDetailsDBVO">
            <meta attribute="implements">com.quinstreet.domain.product.CategoryMasterl10nDetails
            </meta>
            <meta attribute="extends">com.quinstreet.domain.l10n.AbstractL10nDetails
            </meta>
            <meta attribute="implement-equals" inherit="false">true</meta>

            <many-to-one name="localeDbvo"
                class="com.quinstreet.persistence.l10n.LocaleDBVO" column="localeGuid"
                not-null="true" foreign-key="FK_PRD_CATEGORY_MASTER_L10N_LOCALE"
                cascade="none">
                <meta attribute="property-type">com.quinstreet.persistence.l10n.LocaleDBVO
                </meta>
                <meta attribute="use-in-equals">true</meta>
            </many-to-one>
            <many-to-one name="realmDbvo"
                class="com.quinstreet.persistence.context.RealmDBVO" column="realmGuid"
                not-null="true" foreign-key="FK_PRD_CATEGORY_MASTER_L10N_REALM"
                cascade="none">
                <meta attribute="property-type">com.quinstreet.persistence.context.RealmDBVO
                </meta>
                <meta attribute="use-in-equals">true</meta>
            </many-to-one>

            <property name="categoryName" type="string" />

        </composite-element>

    </idbag>

</class>

我得到的错误是:

org.springframework.orm.hibernate3.HibernateSystemException:非法访问加载集合;嵌套异常是 org.hibernate.LazyInitializationException: 非法访问加载集合

在浏览器上我得到错误: org.apache.jasper.JasperException: org.springframework.orm.hibernate3.HibernateSystemException: 集合不与任何会话关联;嵌套异常是 org.hibernate.HibernateException: collection is not associated with any session org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)

我使用 hibernatetemplate 为:

public List<CategoryMasterDBVO> getAllMainCategories() {
return getHibernateTemplate().loadAll(CategoryMasterDBVO.class);
}

请指教。

谢谢

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    当您使用延迟加载的集合加载对象、关闭会话然后访问该集合时,将引发此错误。要么急切地获取集合 (SetFetchMode()),要么保持会话打开

    【讨论】:

    • 我将代码更改为: public List findAllMainCategories() { Criteria c = getSession().createCriteria(CategoryMasterDBVO.class); c.setFetchMode("ProductDbvo", FetchMode.EAGER);列出类别 = c.list();返回类别;但仍然是同样的错误......任何建议......?!
    【解决方案2】:

    我遇到此错误的原因是我在配置文件中配置事务管理的方式,当添加所需的配置(@EnableTransactionManagement)时,问题已解决

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 2013-09-28
      • 2017-12-03
      • 1970-01-01
      • 2018-09-16
      • 2011-05-18
      • 2010-09-18
      相关资源
      最近更新 更多