1

I have the following simple code iterating over a PersistentSet:

if (workers instanceof PersistentSet) {
    for (Object worker : workers) { // failed to lazily initialize a collection of role: ...

    }
}

I get the following exception:

failed to lazily initialize a collection of role...

What is the wrong thing in my code?

1
  • Please send full stack trace, your query and relevant part of model (workers are not just Objects, they are instances of some class that you implemented). Commented Dec 8, 2014 at 7:14

2 Answers 2

1

You need add to your field mapping fetch = FetchType.EAGER.Hibernate use Lazy-loading by default.

Sign up to request clarification or add additional context in comments.

Comments

0

Adding a code snippet on top of @Tkachuk_Evgen's answer to set FetchType.EAGER and retrieve it.

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "mytable", joinColumns = @JoinColumn(name = "e_id"))
private Set<String> employees;
.....

Fetch:

SelectionQuery<QuestionSet> query =  
(SelectionQuery<QuestionSet>) session.createSelectionQuery("from Employee");
    
Set<QuestionSet> set = 
query.getResultStream().collect(Collectors.toSet());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.