我通过服务将dao称为
@Override
@Transactional
public Product getProductById(int id) {
return productDao.getProductById(id);
}
Run Code Online (Sandbox Code Playgroud)
在岛上,我得到的产品
@Override
public Product getProductById(int id) {
Product p = sessionFactory.getCurrentSession().load(Product.class, id);
System.out.print(p);
return p;
}
Run Code Online (Sandbox Code Playgroud)
运行正常,但如果我将dao类更改为
@Override
public Product getProductById(int id) {
return sessionFactory.getCurrentSession().load(Product.class, id);
}
Run Code Online (Sandbox Code Playgroud)
我得到org.hibernate.LazyInitializationException:无法初始化代理-没有会话。例外发生在我只是在打印产品的视图层中。我不明白为什么在dao方法中在同一行中返回会导致视图层出现异常,但是如果将其保存在引用中然后返回它,效果很好。