懒惰初始化休眠中的异常:无法初始化代理 - 无会话
我从我的服务中调用道作为
@Override
@Transactional
public Product getProductById(int id) {
return productDao.getProductById(id);
}
在道中,我得到的产品是
@Override
public Product getProductById(int id) {
Product p = sessionFactory.getCurrentSession().load(Product.class, id);
System.out.print(p);
return p;
}
这运行正常,但如果我把我的道类更改为
@Override
public Product getProductById(int id) {
return sessionFactory.getCurrentSession().load(Product.class, id);
}
我得到 org.hibernate.LazyInitializationException: 无法初始化代理 - 没有会话。例外情况发生在我刚刚打印产品的视图层中。我不明白为什么在dao方法中返回同一行会导致视图层中的异常,但是如果我将其保存在引用中然后返回,则可以正常工作。