Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Devops
  3. Question
Devops

Hibernate cannot simultaneously fetch multiple bags

Asked by Una Manning Jul 15, 2021 2.2K views 1 answer
Share

About this question

Hibernate throws this exception during SessionFactory creation:

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

This is my test case:

Parent.java

@Entity
public Parent {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long id;
 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 // @IndexColumn(name="INDEX_COL") if I had this the problem solve but I retrieve more children than I have, one child is null.
 private List children;
}
Child.java
@Entity
public Child {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long id;
 @ManyToOne
 private Parent parent;
}

How about this problem? What can I do?

EDIT

OK, the problem I have is that another "parent" entity is inside my parent, my real behavior is this:

Parent.java

@Entity
public Parent {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long id;
 @ManyToOne
 private AntoherParent anotherParent;
 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 private List children;
}
AnotherParent.java
@Entity
public AntoherParent {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long id;
 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 private List anotherChildren;
}

Hibernate doesn't like two collections with FetchType.EAGER, but this seems to be a bug, I'm not doing unusual things...

Removing FetchType.EAGER from Parent or AnotherParent solves the problem, but I need it, so real solution is to use @LazyCollection(LazyCollectionOption.FALSE) instead of FetchType (thanks to Bozho for the solution).

Why Hibernate  can not simultaneously fetch multiple bags?

Your answer

1 Answer

More Devops discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest DevOps Blogs

Guides, tips and career advice on DevOps from JanBask experts.