Débora
Débora

Reputation: 5952

How to initialise collections of collections in Hibernate

I get a util.List of instances. each instance has its another collection. I want to initialize all instances of collections. Here is how do it.

Hibernate.initialize(parentList);  

But when the session is closed. parentList's objects'properties can be retrieved. But its collection's instances's properties cannot be retrieved. The way I initialize is wrong or any other problem is there? or how to initialize all instances.?

Upvotes: 3

Views: 8054

Answers (2)

Ahmed MANSOUR
Ahmed MANSOUR

Reputation: 2559

A clean way to work, is to keep using lazy-loading and use Value Object design pattern to transfer data from persistent object to value object and conversely.

Upvotes: 0

mprabhat
mprabhat

Reputation: 20323

Hibernate.initialize(parentList);

will just initialize objects in the list not association inside the list.

From the docs:

Note: This only ensures intialization of a proxy object or collection; it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.

Edit: As per comment

Say if I had one Student Entity and every student Entity has a list of Course Entity. Then the student list can be initialized like this :

for (Student student : studentList) {
     Hibernate.initialize(student.getCourses());
}

Upvotes: 10

Related Questions