Announcement Announcement Module
Collapse
No announcement yet.
Lazy Initialization Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Initialization Exception

    Spent many hours on this problem...hope somebody can help:

    I have a spring hibernate web application using acegi for security.
    I am using an OpenSessionInViewInterceptor
    A dao extends UserDetailsService with the loadUserByUsername function
    My User Details implementation contains a "user" data object

    In my business class, I retrieve this user object from
    SecurityContextHolder.getContext().getAuthenticati on().getPrincipal()

    When I try to lazy load a collection from that user object, I get the dreaded lazy loading exception. However, if I load a fresh user object using the objectid, then lazy loading works on that fresh user object. In other words, lazy loading works on all my objects, but it doesn't work on the user object stored in the Authentication UserDetails object. Is there anything I can do to allow lazy loading on my authentication's user object? Or should I stick to retrieving a fresh copy of the data object before using the collections?

  • #2
    I suspect (don't quote me), that the Principal is an http session-scoped property, and therefore disconnected from your Hibernate session as soon as the Request is resolved (since the OSIV interceptor only opens a Hibernate session for the request, then closes it once the request is rendered). I don't know enough about ACEGI to be sure, but that's the way it would be in container-managed security...

    Comment


    • #3
      Thanks roach, your reasoning matches my suspicions. However, even though you confirmed my suspicions, I have no idea for a "right" solution. That being said, I did find a work around. In my dao loadUserByUsername function, I am now manually initializing the lazy associations using Hibernate.initialize(). The good thing about this work around is that it allows me to keep lazy loading on the object for the general case while avoiding lazy loading for the authentication objects. The bad thing is that I have to remember to add Hibernate.initialize() to any new lazy associations on my user object. This works for me, so I'll consider it solved.

      Comment

      Working...
      X