集合与任何会话无关+引起:org.hibernate.LazyInitializationException:非法访问加载集合

时间:2012-02-08 00:40:58

标签: hibernate

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:collection与任何会话无关;嵌套异常是org.hibernate.HibernateException:collection与任何会话无关     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)

我把hibernatetemplate称为:

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

请建议。

由于

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

最后关注链接帮助... http://forum.springsource.org/archive/index.php/t-20426.html

我改变了hashcode()方法,专门包含了总是被提取的类的字段。

答案 2 :(得分:0)

我对此错误的体验是由于我在配置文件中配置事务管理的方式,当添加所需的配置时@EnableTransactionManagement问题已修复