4 Replies Latest reply on Sep 25, 2008 2:35 PM by nimo22

    Seams EntityManager...

    nimo22

      I have the following Scenario:


      SessionBean A, EntityBean B, ManagedBean C (POJO for my view).


      ONLY in A, I have injected Seams EntityManager:



      public class A implements aLocal {
      
      @In
      EntityManager entityManager;
      ..
      }




      In my View-Bean C, I inject the Interface aLocal and a Instance of B:



      public class C {
      
      //retrieves methods from aLocal
      @In
      aLocal alocal;
      
      @In(required=true) @Out (required = false)
      private B b;
      
        @Begin
        public void changeB {
         b.setAttribute("hello");
      
          }
      ..}




      all works perfectyl, BUT:


      My Seam-managed EntityManager does automatically make a database-operation when I do something like


      b.setName("hello");



      even when I do NOT have explicitly inject a EntityManager in my ManagedBean C or call the method


      entityManager.merge(b)



      As I have no entityManager in C available, I cannot do something like:


      if (entityManager.isOpen())
       { //detach the Entity-Instance} }






      I absolutely want avoid, to inject a EntityManager into a web component (in my case the managedBean C).





      Another point is, when I do in my EntityBean B something like:



      @Entity
      public class B implements Serializable{
      
      private String name;
      
      public String getName() {
              return name.trim();
          }
      



      and call anywhere this getter, then the database-operation UPDATE NAME FROM B is made. Why?


      I do not have call the merge/persist-Method nor I have no entityManager-Instance (in my view-bean).


      Have it to do with call by value and call by reference or with Seams (implicitly extended) EntityManager?? Have it to do with my packaging? I have used SEAM-Gen to generate my EJB-Project.


      I want that my WARs are managed by the servlet-container (having no direct access to the EJBs entityManager, and my EARs (sessionBeans, entityBeans) should be managed by the EJB-Container (having access to the entityManager).


      I need the instance from a entity-Bean and change the values only when I do explicitly want it (with methods such as persist, merge,..).



      Any Ideas?






        • 1. Re: Seams EntityManager...
          wilczarz.wilczarz.gmail.com

          Use


          @Begin( flushMode = FlushModeType.MANUAL )


          No method within the conversation will write the database until you explicitly call entityManager.flush()

          • 2. Re: Seams EntityManager...
            nimo22

            Thanks but this is not the intention what I want to show:


            I have no entityManager-Instance in my View-Bean C and want not to provide a instance as I want to decouple the view from the session-bean.


            I cannot call entityManager.flush() in my View Bean C, as I do not want to provide a EntityManager-Instance in my ManagedBean C.


            I absolutely want to avoid injecting a EntityManager into a web component (in my case the managedBean C).


            I guess, this is the problem:



            //retrieves methods from aLocal
            @In
            aLocal alocal;



            Should I use this instead?:


            //retrieves methods from aLocal
            @EJB
            aLocal alocal;













            • 3. Re: Seams EntityManager...
              swd847
              Your problem is that by default the seam managed persistence context will flush itself to the database automatically, unless you either start a conversation with a flush mode of MANUAL or set the flush mode yourself. There is not need to inject an EntityManager to make this change, you can do it through pages.xml (<begin-conversation flush-mode="MANUAL"/>)
              • 4. Re: Seams EntityManager...
                nimo22

                Thank you, I have done that.