I have an small console application and I am using spring-data-jpa with hibernate. I really can not figure out how to lazy initialize collections when using spring-data-jpa
However, I found a way. This method is inside my service implementation:
public Set fetchUserOrders(Long userId) {
User user = userRepository.findOne(userId);
Hibernate.initialize(user.getOrders());
Set orders = user.getOrders();
return orders;
}
Note: this is as @zagyi sugested in one of his comments, but also pay attention to the statement: Hibernate.initialize(user.getOrders()); without this the collection still wouldn't be initialized and you will get an error.