3 Replies Latest reply on Mar 2, 2010 2:48 PM by Ilya Dyoshin

    manual flushMode in conversation need in session

    nimo stephan Master

      I am using Seam with Hibernate.


      In a Seam-Conversation, I can set the flushMode:


      @Name("myBean")
      @Scope(CONVERSATION)
      public class MyBean{
      
      @Begin(join = true, flushMode = FlushModeType.MANUAL)
      public void do(){..}



      doing this, it will persist or updates all entities after calling explicitly entityMangager.flush.


      My Question is, how can I use this in a Session-Scope?



      @Name("myBean")
      @Scope(SESSION)
      public class MyBean{
      
      // FlushModeType.MANUAL
      // is this the only solution?
      public void do(){
      entityManager.setFlushMode(javax.persistence.FlushModeType.COMMIT);
      
      ...
      // 
      entityManager.flush();
      
      }
      }



      Another question is:


      where lies the difference in using


      entityManager.setFlushMode(javax.persistence.FlushModeType.COMMIT);



      and




      flushMode = FlushModeType.MANUAL

        • 1. Re: manual flushMode in conversation need in session
          Ilya Dyoshin Novice

          the only problem in session bean is to get correct entityManager - that could be achieved by calling Component.getInstance("entityManager", true);  Or Another way -  to move this enetityManager logic to the event, or even Stateless scope, and extend from EntityController superclass.


          If you haven't overrided  the default seam parameters - the default flushmode is Manual ;) so you don't have to do something explicit, except than mark your method as @Transactional.


          Kind regards,


          Ilya Dyoshin

          • 2. Re: manual flushMode in conversation need in session
            nimo stephan Master

            I call my entityManager within a stateless EJB!



            @Stateless
            @Name("myEJB")
            public class MyEJB[
            {
            
            public User getUser{
            
            return entityManager.find(..);
            }
            }




            and getUser is invoked in my ClientBean, which is conversation scoped:




            @Name("myBean")
            @Scope(SESSION)
            public class MyBean{
            
            @In(create=true) MyEJBLocal myEjb;
            
            // FlushModeType.MANUAL
            // is this the only solution?
            public void do(){
            entityManager.setFlushMode(javax.persistence.FlushModeType.COMMIT);
            
            User u = myEjb.getUser(..);
            
            ...
            // 
            entityManager.flush();
            }
            }





            So what do I have to do, when I want to use a Session Bean instead of a Conversation Bean and the Session Bean should have something like


            flushMode = FlushModeType.MANUAL



            ???

            • 3. Re: manual flushMode in conversation need in session
              Ilya Dyoshin Novice

              you can define the method


              getEntityManager() { 
                 return (EnityManager )Contexts.getInstance("entityManager", true);
              }



              and anytime you call this method inside of the session-scoped bean the entityManager would be the most relevant for the current user activity.



              Kind regards,


              Ilya Dyoshin