Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Session + Spring HandlerInterceptor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Session + Spring HandlerInterceptor

    Hello, can you help me?

    I have this two classes
    Code:
    public class PushUserToViewInterceptor extends HandlerInterceptorAdapter {
        private UserBeanDao userService;
        UserSessionManager sessionMgr;
    
        public PushUserToViewInterceptor() {
            sessionMgr = UserSessionManager.getInstance();
        }
    
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            UserBean sessionUser = sessionMgr.getSessionUser(httpServletRequest.getSession(true));
            if (sessionUser != null) {
                userService.reattach(sessionUser);
                modelAndView.addObject("authenticatedUser", sessionUser);
            }
        }
    
        public void setUserService(UserBeanDao userService) {
            this.userService = userService;
        }
    }
    and

    Code:
    public class UserListController implements Controller {
        private UserBeanDao userService;
        private static final String USERS_JSP = "users";
    
        public ModelAndView handleRequest(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws Exception {
            final Map myModel = new HashMap();
            myModel.put("users, userService.getAll());
            return new ModelAndView("listUsers", "model", myModel);
        }
    
        public void setUserService(final UserBeanDao userService) {
            this.userService = userService;
        }
    }
    Code:
        public void reattach(UserBean user) {
            /*
                //just reassociate:
                getHibernateTemplate().lock(user, LockMode.NONE);
    
                //do a version check, then reassociate:
                getHibernateTemplate().lock(user, LockMode.READ);
    
                //do a version check, using SELECT ... FOR UPDATE, then reassociate:
                getHibernateTemplate().lock(pk, LockMode.UPGRADE);
            */
            getHibernateTemplate().lock(user, LockMode.NONE);
        }
    So when i login and go call UserListController i get
    [code]
    net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 4, of class: com.jff.bo.UserBean
    org.springframework.orm.hibernate.SessionFactoryUt ils.convertHibernateAccessException(SessionFactory Utils.java:333)
    org.springframework.orm.hibernate.HibernateAccesso r.convertHibernateAccessException(HibernateAccesso r.java:222)
    org.springframework.orm.hibernate.HibernateTemplat e.execute(HibernateTemplate.java:155)
    org.springframework.orm.hibernate.HibernateTemplat e.lock(HibernateTemplate.java:231)
    com.jff.service.dao.hibernate.UserBeanDaoImpl.reat tach(Unknown Source)
    com.jff.filters.PushUserToViewInterceptor.postHand le(Unknown Source)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:491)
    org.springframework.web.servlet.FrameworkServlet.s erviceWrapper(FrameworkServlet.java:342)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:318)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:743)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
    org.springframework.orm.hibernate.support.OpenSess ionInViewFilter.doFilterInternal(OpenSessionInView Filter.java:117)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:73)
    com.opensymphony.module.sitemesh.filter.PageFilter .parsePage(PageFilter.java:142)
    com.opensymphony.module.sitemesh.filter.PageFilter .doFilter(PageFilter.java:58)
    [code]

    It looks like PushUserToViewInterceptor and UserListController use different hibernate sessions. How can i avoid this?

  • #2
    How are you handling the lifecycle of your Sessions?

    It's either going to be tied to a service layer transaction (via usage of HibernateTransactionManager or JTATransactionManager+HibernateIntercptor), which is the cleanest way, or alternately via the OpenSessionInView filter or interceptor.

    Ideally, all Hibernate related work is done down in your service layer, since that is what is transactionally wrapped, and the Hibernate Session lifecycle is the same as the transaction. This can optionally be combined with the OpenSessionInView filter/interceptor, with the singleSession=false. The latter is still pretty clean, and only means you don't have to touch your lazy relationships so the view layer can get at them.

    Somewhat more convenient, but _much_ less clean, is to use the OpenSessionInView filter/interceptor with the singleSession=true flag. Then everything that happens in the request will use the same session. But it's really not very clean to mix and match objects from different transactions like this (I am assuming you are not wrappng your entire controller in a transaction).

    Comment


    • #3
      Hibernate Session + Spring HandlerInterceptor

      I am using HibernateTransactionManager and OpenSessionInView filter to get lazy-associations, and but i this case here are no transactions and no lazy-associations.
      1 I load specific user and put it in Http session
      2 Display main page
      3 Go to user list page and load all users
      4 excepton.

      I did not change singleSession parameter.

      Comment

      Working...
      X