2011-02-17 64 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

相關問題