8

Trying to upgrade to hibernate 5.2.9 from 4.3.11. Currently using the hibernate native api. After adding the dependency in pom.xml I get the following error when running my units tests:

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

I have a testApplicationContext.xml with the following:

<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" />

I updated the sessionFactory and transactionManager from hibernate4 to hibernate5.

pom.xml:

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

As far as I can tell, the error message implies that there is an issue loading multiple eagerly loaded collections. However, I understand from this that using hibernate-specific annotations and newer versions of hibernate support this use case.

Can anyone help please? Thanks

Community
  • 1
  • 1
Hurricane
  • 1,454
  • 1
  • 13
  • 31

1 Answers1

8

This issue has been resolved now.

In a couple of my entities I was using a List for @OneToMany.

Changing to Set resulted in the error going away. Not sure as yet why this worked/was supported in Hibernate 4.3.11 and not 5.2.9. I will update this answer if I find any further information.

Further info with regard to List vs Set can be found here and here.

Community
  • 1
  • 1
Hurricane
  • 1,454
  • 1
  • 13
  • 31
  • I have of this way @ManyToMany( cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinTable(name = "usuario_bodega", joinColumns = @JoinColumn(name = "id_usuario"), inverseJoinColumns = @JoinColumn(name = "id_bodega")) private Collection listaBodega = new ArrayList(); I don`t have a list I have a collection – Fernando Pie Sep 06 '18 at 16:24
  • The solution of replacing List to Set does work but what is the impact of it? We have more than 1000+ List mappings in our project. Do we need to add @OrderBy or does it depend on the use case? What is the side effect of the getter setters still returning List since we would have do Lists.newArrayList() now instead of returning the reference to the same collection earlier? Thanks ! – Andy Dufresne Feb 12 '19 at 06:59