我叫刀從我的服務爲LazyInitializationException中在休眠:無法初始化代理 - 沒有會話
@Override
@Transactional
public Product getProductById(int id) {
return productDao.getProductById(id);
}
而在DAO我收到的產品爲
@Override
public Product getProductById(int id) {
Product p = sessionFactory.getCurrentSession().load(Product.class, id);
System.out.print(p);
return p;
}
這將運行正常,但如果我更改我的dao類到
@Override
public Product getProductById(int id) {
return sessionFactory.getCurrentSession().load(Product.class, id);
}
我得到org.hibernate.LazyInitializationException:無法初始化代理y - 沒有會話。在我剛剛打印產品的視圖層中發生異常。我不明白爲什麼在dao方法的同一行中返回導致視圖圖層中的異常,但如果將其保存在引用中然後返回它,則工作正常。