防止Hibernate会话通过注释过期? —无法延迟初始化角色的集合

问题描述

| 我有一个休眠的DAO,当尝试访问返回的bag / Collection对象的成员时,它抛出“无法延迟初始化角色的集合”异常。 我了解引发异常的问题的范围。 Hibernate返回我的对象​​,对于任何Collections,返回代理对象。在呼叫者中,由于休眠会话已过期,因此当我访问那些代理对象时,将引发此异常。 我想知道的是,如何使用注释使会话不过期?可能吗? 例如,如果我的调用方法是:
@RequestMapping(\"refresh.url\")
public ModelAndView refresh(HttpServletRequest request,HttpServletResponse response,int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();
如何使用注解防止此异常?我知道一种解决方案是通过回调扩展休眠会话,该回调在伪代码中可能类似于:
@RequestMapping(\"refresh.url\")
public ModelAndView refresh(HttpServletRequest request,int id) throws Exception {
    session = get hibernate session...
    session.doInSession(new SessionCallback() {
        TestObject myObj = testObjDao.get(id);
        // no longer throws the exception
        myObj.getCollection();
    });
但这在我需要访问集合的所有功能中似乎是重复的。没有办法简单地在上面拍一个@Transactional注释并完成它吗?如:
@RequestMapping(\"refresh.url\")
@Transactional  // this doesn\'t extend the session to this method?
public ModelAndView refresh(HttpServletRequest request,int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();
多谢您协助我解释这个问题。     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)