原因:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包

use*_*956 3 spring hibernate jpa

我需要配置spring + JPA(EntityManager)+ Hibernate。
如果必须获取= FetchType.LAZY运行服务器成功
如果必须获取= FetchType.LAZY运行服务器错误:
我使用的是tomcat 7
原因:org.springframework.beans.factory.BeanCreationException:创建名称为'entityManagerFactory'的bean时出错在ServletContext资源[/WEB-INF/applicationContext.xml]中定义:调用init方法失败;嵌套的异常是javax.persistence.PersistenceException:[PersistenceUnit:fmis2]无法构建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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Java代码道

 public abstract class CommonDao {

        @PersistenceContext(unitName = "fmis2")
        protected EntityManager entityManager;
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

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

  • 使用LAZY列表。如果您需要“模拟”一个渴望的关系,请在查询之前使用yourList.size()进行填充;
  • 在数据结构中使用“设置”代替“列表”。

其他说明: Hibernate无法同时获取多个包

使用JPA在Hibernate中使用EAGER类型的多次获取

问候。