I have 2 entities related to each other like this:
EntityA {
(...)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "entityA")
private List<EntityB> listOfBs;
(...)
// getters and setters
}
EntityB {
(...)
@ManyToOne
@JoinColumn(name = "idEntityA)
private EntityA entityA;
(...)
// getters and setters
}
The related business rule is: EntityA can have one or more EntityB, hence the @OneToMany
. Each EntityB can only be assigned to one EntityA. Initially.
The application's information flow is this - first you get a list of EntityA, then you select one EntityA so you can get a list of all EntityBs associated to the selected EntityA.
The problem is: If I have only one EntityB per EntityA, I get the correct behavior on the first screen. If I have more than one EntityB associated to an EntityA, the first screen shows a number of EntityA equals to the number of EntityB associated with it.
But if I change the annotation of EntityA from @OneToMany(fetch = FetchType.EAGER, mappedBy = "entityA")
to @OneToMany(fetch = mappedBy = "entityA")
, the correct behavior is shown in the first screen (one entityA regardless how many entityBs are associated with it), but when I try to access it I get the following error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: b.c.m.h.m.EntityA.listOfBs, could not initialize proxy - no Session
What's going on here?
EDIT - The solution offered on the other question does not solves my issue.