2

i have a exception when i try to fetch from the database.

I have three entities with a ManyToOne relation. One A -> Many B, One A -> C and One B -> Many D. I use the EAGER'Type. but i get exception. See below.

The first entity:

@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;

The second one:

@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;

The last one:

@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;

when i run the query, i got this execption:

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)

when i change the fetch'type to LAZY, i get this execption:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: B.d, could not initialize proxy - no Session

Any idea? Thanks ;-)

emoleumassi
  • 4,881
  • 13
  • 67
  • 93
  • Resolve with: @LazyCollection(LazyCollectionOption.FALSE), see [link][1] [1]: http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags – emoleumassi Mar 26 '15 at 22:43

1 Answers1

1

Using @LazyCollection(LazyCollectionOption.FALSE) should solve your issue, please refer to Hibernate cannot simultaneously fetch multiple bags for more details

Community
  • 1
  • 1