Hibernate lanza esta excepción durante la creación de SessionFactory:
org.hibernate.loader.MultipleBagFetchException: no puede recuperar simultáneamente varias bolsas
Este es mi caso de prueba:
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<Child> children;
}
Child.java
@Entity
public Child {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Parent parent;
}
¿Qué tal este problema? ¿Que puedo hacer?
EDITAR
OK, el problema que tengo es que hay otra entidad "padre" dentro de mi padre, mi comportamiento real es este:
Parent.java
@Entity
public Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private AnotherParent anotherParent;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
private List<Child> children;
}
AnotherParent.java
@Entity
public AnotherParent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
private List<AnotherChild> anotherChildren;
}
A Hibernate no le gustan dos colecciones FetchType.EAGER, pero esto parece ser un error, no estoy haciendo cosas inusuales ...
La eliminación FetchType.EAGERdel Parento AnotherParentresuelve el problema, sino que lo necesita, por lo que verdadera solución es utilizar @LazyCollection(LazyCollectionOption.FALSE)en lugar de FetchType(gracias a Bozho para la solución).
select * from master; select * from child1 where master_id = :master_id; select * from child2 where master_id = :master_id
List<child>con la fetchTypedefinida por más de una List<clield>
