尝试升级到 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
导致错误消失。尚不确定为什么这在 Hibernate 4.3.11 而不是 5.2.9 中有效/受支持。如果我找到任何进一步的信息,我会更新这个答案。
有关 List
的更多信息对比 Set
可以找到 here和 here .
关于spring - Hibernate 4.3 到 5.2 升级 - 不能同时取多个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044818/