【发布时间】:2016-12-27 12:13:12
【问题描述】:
我将 dao 从我的服务中称为
@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:无法初始化代理 - 没有会话。异常发生在我刚刚打印产品的视图层中。我不明白为什么在 dao 方法中返回同一行会导致视图层出现异常,但如果我将其保存在引用中然后返回它,则可以正常工作。
【问题讨论】: