Error in ManyToMany relationship in Hibernate

1

I have a problem with @manyToMany . It's giving the error:

  

org.hibernate.loader.MultipleBagFetchException: can not simultaneously fetch multiple bags

Follow my template:

@Entity
@Table(name = "sar_evento", schema = "sar")
@SequenceGenerator(name = "sequence", sequenceName = "sar.evento_sequence", schema = "sar")
public class Evento implements Serializable, BaseEntity {

    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_evento", nullable = false, unique = true)
    private Long id;

    @Column(length = 255)
    private String nome;

    @Column(columnDefinition = "text")
    private String descricao;

    @Column(name = "data_inicio")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataInicio;

    @Column(name = "data_fim")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataFim;

    @Column(name = "dia_todo")
    private boolean diaTodo;

    @ManyToOne
    @JoinColumn(name = "id_sala", referencedColumnName = "id_sala")
    private Sala sala;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @OrderBy("nome asc")
    @JoinTable(name = "sar_evento_recurso", schema = "sar",
            joinColumns = {
                @JoinColumn(name = "id_evento")},
            inverseJoinColumns = {
                @JoinColumn(name = "id_recurso")})
    private List<Recurso> recursos = new ArrayList<Recurso>();

    @ManyToOne
    @JoinColumn(name = "id_usuario", referencedColumnName = "id")
    private Usuario usuario;

Has anyone ever had this problem?

    
asked by anonymous 20.10.2014 / 20:46

1 answer

1

This error refers to cases where a query in JPA or Hibernate uses fetch join in more than one relationship.

"normal" joins, lazy and eager, bring the related lists into SELECT commands subsequent to the SELECT principal, that is, it retrieves the requested entity first, then the their relationships.

This means that, in terms of performance, lazy mode and eager mode are equivalent. The difference is that lazy expects a getter method to be invoked to execute the other queries, whereas in eager mode they are executed shortly after the main .

fetch join forces a native in the database, already recovering both the parent entity and related entities. Usually this is used for optimization purposes.

However, Hibernate only supports fetch join in a related table. If you try to do more than one, the indicated error will occur.

The reason is very simple when you think about the result of query with multiple joins.

For example, let's assume you try to retrieve a list of the Evento entity and do the fetch join with Recurso . If there are N events and M resources for each event the query returns N x M results, in which Evento data will be repeated for each record of its Recurso .

Now, if we try to add some more relationship, for example Sala , its values will also be repeated N x M x K times.

In addition to the bad query performance, another problem emerges: it is very complicated to process the result of the query and identify which values repeat and which should create another entity in the list.

The solution is to recover these relationships in separate queries and not try to do everything at once.

    
20.10.2014 / 21:08