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

  • Hibernate Session + Spring HandlerInterceptor

    Hello, can you help me?

    I have this two classes
    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) {
                modelAndView.addObject("authenticatedUser", sessionUser);
        public void setUserService(UserBeanDao userService) {
            this.userService = userService;

    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;
        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
    net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 4, of class:
    org.springframework.orm.hibernate.SessionFactoryUt ils.convertHibernateAccessException(SessionFactory
    org.springframework.orm.hibernate.HibernateAccesso r.convertHibernateAccessException(HibernateAccesso
    org.springframework.orm.hibernate.HibernateTemplat e.execute(
    org.springframework.orm.hibernate.HibernateTemplat e.lock(
    com.jff.service.dao.hibernate.UserBeanDaoImpl.reat tach(Unknown Source)
    com.jff.filters.PushUserToViewInterceptor.postHand le(Unknown Source)
    org.springframework.web.servlet.DispatcherServlet. doService(
    org.springframework.web.servlet.FrameworkServlet.s erviceWrapper(
    org.springframework.web.servlet.FrameworkServlet.d oGet(
    javax.servlet.http.HttpServlet.service(HttpServlet .java:743)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:856) ionInViewFilter.doFilterInternal(OpenSessionInView
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(
    com.opensymphony.module.sitemesh.filter.PageFilter .parsePage(
    com.opensymphony.module.sitemesh.filter.PageFilter .doFilter(

    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).


    • #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.