Reputation: 24667
I'm trying to use HibernateDaoSupport but I'm getting stuck with a org.hibernate.LazyInitializationException problem.
This is an example of what I want to do;
public class MyDaoImpl extends HibernateDaoSupport {
public Set<Long> getCoreItemIdsForCustomerIds(Set<Long> customerIds) {
Set<Long> itemIds = new HashSet<Long>();
for (Long customerId : customerIds) {
Customer customer = getCustomerWithId(customerId);
itemIds.addAll(getItemIdsFromItems(customer.getCoreItems()));
}
return itemIds;
}
private Customer getCustomerWithId(Long customerId) {
return getHibernateTemplate().get(Customer.class, customerId);
}
private Set<Long> getItemIdsFromItems(Set<Item> items) {
Set<Long> itemIds = new HashSet<Long>();
for (Item item : items) {
itemIds.add(item.getId());
}
return itemIds;
}
}
A Customer has a collection of Items. The entity is Fetched lazily, so I guess the problem is after getCustomerWithId completes the session is closed, and 'customer' is now detached. So when customer.getCoreItems() is invoked the exception is thrown.
Does any one know how I can use HibernateDaoSupport and keep the session open until getCoreItemIdsForCustomerIds returns?
Or do I need to start and close the transaction manually myself to do this?
Hope that has made sense! Thanks.
Upvotes: 1
Views: 453
Reputation: 73484
Use the OpenSessionInView filter. The session will be open for the duration of the request-response render phase.
Upvotes: 1