3 Replies Latest reply on Sep 17, 2007 5:51 AM by Wolfgang Schwendt

    reasociating detached entities (non smpc)

    C B Newbie

      Because I am using Seam only for the presentation layer (and Spring for the rest) obviously I have no seam managed persistence context. If I would nevertheless like to store (hibernate) entities inside my long running conversations I run into many problems. I always get LazyInitializationExceptions because my entities from a previous request (but from the same conversation) are no longer associated with the current session.

      What is the easiest way to manually re-associate all detached entities in my conversation with the new session of the current request? I tried to study the Seam source code but did not discover which trick Seam uses to achieve this so smoothly an transparent.

      FYI: I'm using Seam 2.0.0.BETA1 and Hibernate 3 (and Spring 2).

        • 1. Re: reasociating detached entities (non smpc)
          Jacob Orshalick Apprentice

          If it is okay to copy the state of the given object onto the persistent object with the same identifier, the following will do the trick:

           ...
           attachedEntity = myHibernateSession.merge(detachedEntity);
           ...
          


          Be aware of the semantics of the merge operation defined by JSR-220.

          Otherwise, you could simply do a fetch join when you retrieve the object initially to also retrieve required dependencies.

          • 2. Re: reasociating detached entities (non smpc)
            C B Newbie

            Thanks for the hint, jacob.orshalick. Actually, the problem is however not about re-associating a single know entity to the new session but rather about re-associating all entities in the current long running transaction. So the challenge is much rather how to find / enumerate all entity objects that exist in the current transaction.

            btw: I already knew session.merge() and a few other hibernate tricks that re-associate a single entity, but a "grep" through the Seam source (Seam2.0.0.BETA1) had shown me that Seam does not actually use merge() to achieve the goal even before I started this thread.

            • 3. Re: reasociating detached entities (non smpc)
              Wolfgang Schwendt Newbie

               

              "cba2" wrote:
              What is the easiest way to manually re-associate all detached entities in my conversation with the new session of the current request? I tried to study the Seam source code but did not discover which trick Seam uses to achieve this so smoothly an transparent.


              Seam supports the concept of a Seam-managed conversation-scoped persistence context, which is an extended persistence context whose lifetime is tied to the lifetime of a conversation.

              If such a Seam-managed persistence context is used, Entities don't get detached between requests but stay managed as long as the conversation (or application transaction) lasts.

              See chapter "Chapter 8. Seam and Object/Relational Mapping", section
              "8.3. Seam-managed persistence contexts" of the reference manual for more information.