无法延迟初始化角色集合-尽管hbm.xml中有lazy =“ false”

时间:2019-10-03 10:22:21

标签: java hibernate fetch

即使我在lazy="false"中有UserLimits.hbm.xml,我仍然不断收到错误消息:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.project.eu.model.UserModel.userLimits, could not initialize proxy - no Session

userLimits不会被抢购吗?

以下是一些代码段。一种通过customerId获取用户的方法:

public List<UserModel> getUsersForCustomer(final Long customerId) {
    List<UserModel> users = getHibernateTemplate().execute(
            session -> {
                Criteria criteria = session.createCriteria(UserModel.class);
                criteria.add(Restrictions.eq("customerId", customerId));
                return criteria.list();
            });
    return users;
}

User.hbm.xml

<hibernate-mapping>
    <class name="com.project.eu.model.UserModel" table="USR_USERS" batch-size="20">
        <cache usage="transactional"/>
        <id name="id" type="long">
            <column name="ID" precision="19" scale="0" />
            <generator class="sequence">
                <param name="sequence_name">ID_SEQ</param>
            </generator>
        </id>
        <property name="customerId" type="java.lang.Long">
            <column name="CUSTOMER_ID" precision="19" scale="0" />
        </property>
        <bag name="userLimits" cascade="delete-orphan" >
            <key column="USER_ID" />
            <one-to-many class="com.project.eu.model.UserLimitsModel" />
        </bag>
    </class>
</hibernate-mapping>

UserLimits.hbm.xml

<hibernate-mapping>
    <class name="com.project.eu.model.UserLimitsModel" table="USER_LIMITS" lazy="false">
        <cache usage="transactional"/>
        <id name="id" type="long">
            <column name="ID" precision="19" scale="0" />
            <generator class="sequence">
                <param name="sequence_name">ID_SEQ</param>
            </generator>
        </id>
        <version name="version" type="java.lang.Long" >
            <column name="VERSION" precision="10" scale="0" />
        </version>
        <property name="userId" type="java.lang.Long" >
            <column name="USER_ID" precision="19" scale="0" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

怎么了?

0 个答案:

没有答案