我不明白为什么会这样。基于代码路径,当引发此异常时,我应该在相同的线程内并且会话应该存在。
有人可以告诉我这里缺少什么吗?
我已经设置
<property name="hibernate.current_session_context_class">thread</property>
在hibernate.cfg.xml文件中
我在Servlet过滤器
中创建了以下代码 try{
factory.getCurrentSession().beginTransaction();
httpRequest.getRequestDispatcher("/public/index.html").forward(httpRequest,response);
}finally{
factory.getCurrentSession().getTransaction().commit();
}
在index.xhtml文件中,我有以下调用:
做一些事情
index.xhtml使用的模板使用ui include来加载menu.xhtml文件。菜单文件然后插入menulist。 来自menuItemsViewController的servicesMenuItems调用最终会在以下代码中结束
public Collection<Bulletin> getBulletin(User bean){
Session session=factory.getCurrentSession();
try{
session.refresh(bean);
if(bean.getObligations().size()>0){
do some stuff
}
正如您可以看到会话存在,当session.refresh(bean)
未抛出异常但bean.getObligations()
正在抛出时,
未能懒惰地初始化角色集合: data.User.obligations,无法初始化代理 - 没有会话“
任何想法为什么?
答案 0 :(得分:0)
默认的lazy属性为true。这意味着hibernate不会填充实体上的对象属性。在这种情况下,Hibernate不会在User bean中填充Obligation。当您尝试获得义务时,将抛出异常。
Hibernate Eager vs Lazy Fetch Type
您可以更改懒惰财产或必须由您自己填写义务。
答案 1 :(得分:0)
好的..我修复了这个问题,但我仍然不知道是什么导致它... 仔细检查我的代码后,我注意到我正在将用户放入会话中......并且在第一次尝试时,一切正常,第二次尝试浏览页面时,发布了异常......
我开始清理会话并在每次尝试访问页面时将用户置于会话中..现在我再也看不到例外了......