【问题标题】:Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags引起:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包
【发布时间】:2015-11-28 17:48:45
【问题描述】:

我需要配置 spring + JPA (EntityManager) + Hibernate 。
如果我必须 fetch = FetchType.LAZY 运行服务器成功
如果我不得不 fetch = FetchType.EAGER 运行服务器错误:
我使用tomcat 7
原因:org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/applicationContext.xml] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: fmis2] Unable to build EntityManagerFactory
...
原因:javax.persistence.PersistenceException: [PersistenceUnit: fmis2] 无法构建 EntityManagerFactory
...
引起:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包

请帮助我。我错在哪里。 谢谢

配置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 代码实体

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 代码道

 public abstract class CommonDao {

        @PersistenceContext(unitName = "fmis2")
        protected EntityManager entityManager;
    }

【问题讨论】:

    标签: spring hibernate jpa


    【解决方案1】:

    问题在于 Hibernate 规范:他不允许使用 EAGER 标注的多个列表。有一些选项可以绕过这个问题:

    • 使用 LAZY 列表。如果您需要“模拟”急切的关系,请在查询之前使用 yourList.size() 进行填充;
    • 在您的数据结构中使用 Set 而不是 List。

    其他说明: Hibernate cannot simultaneously fetch multiple bags

    Multiple fetches with EAGER type in Hibernate with JPA

    问候。

    【讨论】:

      猜你喜欢
      • 2014-08-31
      • 2016-09-13
      • 1970-01-01
      • 2013-05-03
      • 2011-05-19
      • 2017-08-20
      • 2021-11-24
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多