Reputation: 3
I have a problem with write correct JPA query to get entity with one filled collection. For example I have entity with two collections (I want to have FetchType.LAZY for both collections):
public class Entity {
private String value;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="entity_id")
private List<ElementA> elementsA;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="entity_id")
private List<ElementB> elementsB;
}
And I want to run JPA query and get only information about Entity and collection 'elementsA'. I was trying with:
"From Entity e join fetch e.elementsA eA"
But I've got error:
Caused by: org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role:
model.jpa.entities.Entity.elementsB,
could not initialize proxy - no Session
How I should write this query to fetch only information about collection 'elementsA'.
Thanks, Darek.
Upvotes: 0
Views: 360
Reputation: 5341
Your fetching is correct, but your exception is indicating that later after session is closed, somewhere in the code you're calling entity.getElementsB();
which triggers another query/fetch, which results in LazyInitializationException
because session is closed at that point.
Upvotes: 3