zhaoweianan 2008年09月12日
困扰我几天的一个延迟加载问题
<!--初始化WebApplicationContext-->
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>/WEB-INF/classes/applicationContext.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<!--open session in view -->
<filter>
<filter-name>hibernateFilter </filter-name>
<filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
<init-param>
<param-name>singleSession </param-name>
<param-value>true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter </filter-name>
<url-pattern>*.jsp </url-pattern>
</filter-mapping>

调用代码User与Message是一对多的关系
public Users queryUsers(final int id){
return (Users)getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session)
throws HibernateException, SQLException {

String hql = "from Users where id=:id ";
Query query = session.createQuery(hql);
query.setInteger("id",id);
List <Users> list = query.list();
return list.get(0);
}
});
}
public class UserAction extends Action {

private TestDao testDao;

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

Users user = testDao.queryUsers(1);
System.out.println(user.getMessages().size());
return mapping.findForward("ok");
}
}

执行代码抱错...Session is closed...
求高手帮忙解决呀,困扰我很多天了
...全文
77 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java EE
创建于2007-09-28

3.7w+

社区成员

22.5w+

社区内容

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区公告
暂无公告