gpt4 book ai didi

spring - 由 : org. hibernate.loader.MultipleBagFetchException 引起:无法同时获取多个包

转载 作者:行者123 更新时间:2023-12-03 23:15:11 24 4
gpt4 key购买 nike

我需要配置 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] 无法构建 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;
}

最佳答案

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

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

  • 其他解释:
    Hibernate cannot simultaneously fetch multiple bags

    Multiple fetches with EAGER type in Hibernate with JPA

    问候。

    关于spring - 由 : org. hibernate.loader.MultipleBagFetchException 引起:无法同时获取多个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366376/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com