当我尝试从数据库中提取时,我有一个例外。
我有三个具有ManyToOne关系的实体。一个A - >许多B,One A - > C和一个B - >很多D.我使用的是EAGER' Type。但我得到例外。见下文。
第一个实体:
@Entity
@Table
public class A {
@Id
@Column(columnDefinition = "VARCHAR(50)", nullable = false)
private String aId;
@OneToMany(mappedBy = "a", targetEntity = B.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<B> b;
@OneToMany(mappedBy = "a", targetEntity = C.class, cascade = CascadeType.ALL)
private List<C> c;
第二个:
@Entity
@Table
public class B {
@Id
@Column(columnDefinition = "VARCHAR(50)", nullable = false)
private String bId;
@ManyToOne(targetEntity = A.class, cascade = CascadeType.ALL)
@JoinColumn(name = "aId", referencedColumnName = "aId")
private A a;
@OneToMany(mappedBy = "b", targetEntity = D.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<D> d;
最后一个:
@Entity
@Table
public class D {
@Id
@Column(columnDefinition = "VARCHAR(50)", nullable = false)
private String dId;
@ManyToOne(targetEntity = B.class, cascade = CascadeType.ALL)
@JoinColumn(name = "bId", referencedColumnName = "bId",)
private B b;
当我运行查询时,我得到了这个执行:
Invocation of init method failed; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at de.emo.cit.tuberlin.GetController.getUDDISLA(GetController.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
当我将fetch&#39;类型改为LAZY时,我得到了这个例子:
引起:org.hibernate.LazyInitializationException:懒得初始化角色集合:B.d,无法初始化代理 - 没有会话
有什么想法吗? 谢谢; - )
答案 0 :(得分:1)
使用@LazyCollection(LazyCollectionOption.FALSE)
可以解决您的问题,请参阅Hibernate cannot simultaneously fetch multiple bags了解详情