1
I have a problem with @manyToMany
. You’re making the mistake:
org.hibernate.Multiplebagfetchexception: cannot simultaneously fetch Multiple bags
Follows my model:
@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;
someone’s been through this trouble?
Only by complementing, changing from List to Set should solve the problem: http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags
– Josh
Hello I changed my relationship to:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @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>();
. Now I’m having trouble with session:org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
.– paulohddias
@Josuéeduardo Apparently, change to
Set
really avoids the problem, but it is important to emphasize thatSet
andList
are not equivalent structures. In the case of this issue, there is a@OrderBy
in the relationship. With theSet
, there is no guarantee that the order will be respected.– utluiz
@paulohddias Try to solve by going back to
FetchType.EAGER
and adding the annotation@Fetch(FetchMode.SELECT)
. This will cause the list to be retrieved in aSELECT
later, avoiding problems with joins.– utluiz
I switched to
:@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 @Fetch(FetchMode.SELECT)
 @JoinTable(name = "sar_evento_recurso", schema = "sar",
 joinColumns = {
 @JoinColumn(name = "id_evento")},
 inverseJoinColumns = {
 @JoinColumn(name = "id_recurso")})
 private Set<Recurso> recursos = new HashSet<Recurso>();
. But I keep having session error when I try to change or delete, to insert this all ok.– paulohddias
@paulohddias Regarding the error of
LazyInitializationException
, are you sure it’s not in other relationships? Try puttingEAGER
for everything you are using in the change and removal.– utluiz
All with EAGER
– paulohddias
and the mistake is in this relationship because without it I get normal
– paulohddias
@paulohddias The code that is in your question is not all with ager. Also, the fact that you take out this relationship makes the code work doesn’t mean that the mistake is just in it. As I explained in the answer, error occurs when there is more than one relationship.
– utluiz
so the start error no longer happens when I switched to set and put @Fetch(Fetchmode.SELECT) the problem is now in the session I am using jpa and I tried to create opensessionview and it is no use
– paulohddias
I’m lost I don’t know what to do
– paulohddias
I got thank you the error was in the view in jsf, of course after I tidied up manyTomany as you said I had to put not my f:selectItems the <f:attribute name="collectionType" value="java.util.Hashset" /> dai got the session error thank you very much for the help
– paulohddias