spring - Hibernate 4.3 到 5.2 升级 - 不能同时取多个包

标签 spring hibernate

尝试升级到 hibernate 5.2.9 来自 4.3.11 .当前使用 hibernate native api。在 pom.xml 中添加依赖项后,运行单元测试时出现以下错误:

Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [testApplicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags



我有一个 testApplicationContext.xml 具有以下内容:
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="org.xxxx.xxxx.xxxx.model"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

我将 sessionFactory 和 transactionManager 从 hibernate4 更新为 hibernate5。

pom.xml :
<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.9.Final</version>
    </dependency>

据我所知,错误消息意味着加载多个急切加载的集合时出现问题。但是,我从 this 了解到使用特定于 hibernate 的注释和较新版本的 hibernate 支持此用例。

有人可以帮忙吗?谢谢

最佳答案

这个问题现在已经解决了。

在我的几个实体中,我使用了 List@OneToMany .

更改为 Set导致错误消失。尚不确定为什么这在 H​​ibernate 4.3.11 而不是 5.2.9 中有效/受支持。如果我找到任何进一步的信息,我会更新这个答案。

有关 List 的更多信息对比 Set可以找到 herehere .

关于spring - Hibernate 4.3 到 5.2 升级 - 不能同时取多个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044818/

相关文章:

java - Spring MVC 匹配通配符严格错误

java - 如何在 Spring 表达式语言中引用常量

spring - 如何使用 redis 模板从 Redis 获取所有 key

java - Hibernate ConfigurationException : Could not locate cfg. xml 资源 [hibernate.cfg.xml] IntelliJ

java - 对事务提交执行操作

java - 在appengine中使用ajax上传文件时出现问题

java - 部署 Spring MVC 项目

java - Hibernate查询转Sql查询

java - JPA CriteriaBuilder 获取具有条件的列表实体

java - 使用注释将 hibernate 与 Spring 集成时出现错误