我从我的服务中调用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:无法初始化代理 - 没有Session。异常出现在视图层,我只是打印产品。我不明白为什么在dao方法中同一行返回结果会导致视图层中的异常,但如果我将其保存在引用中然后返回它,则可以正常工作。