2011-02-17 60 views
1

我想使用HibernateDaoSupport,但我陷入了一个org.hibernate.LazyInitializationException问题。LazyInitializationException使用HibernateDaoSupport

这是我想要做的一个例子;

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; 
    } 
} 

客户拥有一系列物品。该实体懒惰地获取,所以我猜问题是getCustomerWithId完成会话关闭后,'客户'现在分离。因此,当customer.getCoreItems()被调用时,引发异常。

是否有人知道如何使用HibernateDaoSupport并保持会话打开,直到getCoreItemIdsForCustomerIds返回?

或者我是否需要自己手动启动和关闭事务来执行此操作?

希望有道理!谢谢。

回答

1

使用OpenSessionInView过滤器。会议将在请求 - 响应呈现阶段期间开放。

+0

是的,我读过我早些时候谷歌搜索的时候。这不是真正的解决方案,因为在我看来,我不会尝试访问懒惰加载的属性。 – C0deAttack 2011-02-17 16:37:00

相关问题