4 Replies Latest reply on Jul 28, 2009 6:19 AM by jk;l jkl;

    Merging necessary for detached association?

    jk;l jkl; Expert

      I have a conversation scoped entity that needs to be persisted. It has an association to a session-scoped entity loaded in a previous conversation, which is therefore a detached entity with respect to the current EntityManager.


      Must I merge as follows?



      sessionEntity = entityManager.merge(sessionEntity);
      conversationEntity.setAssociation(sessionEntity);
      entityManager.persist(conversationEntity);




      Or is there any way to remove the initial merge? I don't care whether sessionEntity contains the most updated data from the database, I just want the foreign keys to be set up in the database correctly.


      I don't want the merge to make an extra trip to the database. I tried doing an entityManager.getReference(SessionEntity.class, sessionEntityId), but that also seems to make a trip to the database.


      I don't want to rewrite every query to use straight JDBC just so I avoid unnecessary trips to the database - how can I get around this issue?

        • 1. Re: Merging necessary for detached association?
          Arbi Sookazian Master

          Honestly, I think you're the first person I've seen in this forum that is actually using an entity class with a non-default (i.e. non-conversation) scope.  That's not necessarily a bad thing but why are you using a session-scoped entity class?


          Why don't you just outject a List<foo> as a result of a JPQL query or similar to session scope instead?


          Are you using that entity class as a backing bean with a @Name annotation?


          There is an example in SiA book (see Listing 4.1) with @Scope(ScopeType.EVENT) on an entity class.  Are you doing something similar?

          • 2. Re: Merging necessary for detached association?
            Arbi Sookazian Master

            The booking example has a session-scoped entity class:


            @Entity
            @Name("user")
            @Scope(SESSION)
            @Table(name="Customer")
            public class User implements Serializable
            {...}



            But it is being outjected to session scope in the AuthenticatorAction:


            @Stateless
            @Name("authenticator")
            public class AuthenticatorAction 
                implements Authenticator
            {
                @PersistenceContext 
                private EntityManager em;
            
                @In(required=false)   
                @Out(required=false, scope = SESSION)
                private User user;
               
                public boolean authenticate()
                {
                 List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
                                     .getResultList();
                  
                 if (results.size()==0) {
                     return false;
                 } else {
                     user = (User) results.get(0);
                     return true;
                 }
                }
                
            }
            



            I'm not sure that the entity class needs to be session-scoped if the outjection to session scope is there...

            • 3. Re: Merging necessary for detached association?
              jk;l jkl; Expert

              The session-scoped entity, as in your example, is a User. However, I haven't defined the @Scope annotation on the entity - I've outjected the user to a session scope from an authentication method.


              In cases where a session-scoped entity is an attribute of a conversation-scoped entity about to be persisted, I don't want to update the contents of the session-scoped entity from the database.


              It seems the only way to do this would be to use straight JDBC.


              I really hope there's a simple way to do what I'm trying using Seam and Hibernate.

              • 4. Re: Merging necessary for detached association?
                jk;l jkl; Expert

                Any Seam user who uses a session-scoped Member/User object would have come across this issue.


                Either they're living with the inefficiency of a separate database access for the merge of the session-scoped Member/User, or they have found a way around it - if the latter, please share :)