Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

I need to configure spring + JPA (EntityManager) + Hibernate .
If I had to fetch = FetchType.LAZY run server success
If I had to fetch = FetchType.EAGER run server error:
I using tomcat 7
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: fmis2] Unable to build EntityManagerFactory
...
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: fmis2] Unable to build EntityManagerFactory
...
Caused by: org.hibernate.loader.MultipleBagFetchException: can not simultaneously fetch multiple bags

Please help me. Where I was wrong. Thanks

Config applicationContext.xml

<context:annotation-config />
<context:component-scan base-package="com.evnit.fmis" />
<jpa:repositories base-package="com.evnit.fmis" />
<!-- START -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml" />
    <property name="persistenceUnitName" value="fmis2" />
    <property name="dataSource" ref="fmis2dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="database" value="SQL_SERVER" />
        </bean>
    </property>
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>

</bean>

<bean id="fmis2dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

jpa-persistence.xml

<persistence-unit name="fmis2" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jar-file>/WEB-INF/lib/accounting-inf-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/masterdata-inf-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/congno-backend-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/congcudungcu-backend-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/taisan-backend-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/vattu-backend-1.0-SNAPSHOT.jar</jar-file>
        <jar-file>/WEB-INF/lib/muahang-backend-1.0-SNAPSHOT.jar</jar-file>
</persistence-unit>

Java Code entity

package com.evnit.fmis.accounting.entity;

@Entity
@Table(name = "ChungTu", schema = "ketoan")
public class ChungTu implements java.io.Serializable {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "chungTu")
    public List<DinhKhoan> getDinhKhoans() {
        return this.dinhKhoans;
    }

    public void setDinhKhoans(List<DinhKhoan> dinhKhoans) {
        this.dinhKhoans = dinhKhoans;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "chungTu")
    public List<Uynhiemchi> getUynhiemchis() {
        return this.uynhiemchis;
    }

    public void setUynhiemchis(List<Uynhiemchi> uynhiemchis) {
        this.uynhiemchis = uynhiemchis;
    }
}
@Entity
@Table(name = "DinhKhoan", schema = "ketoan")
public class DinhKhoan implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private ChungTu chungTu;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "IdChungtu")
    public ChungTu getChungTu() {
        return this.chungTu;
    }

    public void setChungTu(ChungTu chungTu) {
        this.chungTu = chungTu;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "dinhKhoan")
    public List<HoaDonVat> getHoaDonVats() {
        return this.hoaDonVats;
    }

    public void setHoaDonVats(List<HoaDonVat> hoaDonVats) {
        this.hoaDonVats = hoaDonVats;
    }

}
@Entity
@Table(name = "Uynhiemchi", schema = "ketoan")
public class Uynhiemchi implements java.io.Serializable {

    private ChungTu chungTu;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "IdChungtu", nullable = true)
    public ChungTu getChungTu() {
        return this.chungTu;
    }

    public void setChungTu(ChungTu chungTu) {
        this.chungTu = chungTu;
    }

}

@Entity
@Table(name = "HoaDonVAT", schema = "ketoan")
public class HoaDonVat implements java.io.Serializable {

    private DinhKhoan dinhKhoan;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "IdDinhKhoan")
    public DinhKhoan getDinhKhoan() {
        return this.dinhKhoan;
    }

    public void setDinhKhoan(DinhKhoan dinhKhoan) {
        this.dinhKhoan = dinhKhoan;
    }
}

Java Code Dao

 public abstract class CommonDao {

        @PersistenceContext(unitName = "fmis2")
        protected EntityManager entityManager;
    }
like image 717
user1550956 Avatar asked Sep 03 '15 03:09

user1550956


1 Answers

The problem is the Hibernate specification: he doesn't allow more than one list noted with EAGER. There are some options to bypass this problem:

  • Use LAZY lists. If you need 'to simulate' a eager relation, use yourList.size() to populate before the query;
  • Use Set instead List in your data structures.

Other explanations: Hibernate cannot simultaneously fetch multiple bags

Multiple fetches with EAGER type in Hibernate with JPA

Regards.

like image 74
Vilmar César Pereira Júnior Avatar answered Sep 23 '22 02:09

Vilmar César Pereira Júnior