2 Replies Latest reply on Apr 14, 2009 12:42 AM by Brandon Simpson

    Need help: Trying to load data from DB, edit it, then save.

    Brandon Simpson Newbie
      It sounds easy enough, and I thought it would be until I started trying to implement it. The nature of my page is that it is highly interactive, the user can click on Yes/No buttons to selectively hide/show other parts of a really large page. This functionality uses ICEFaces so the state and rendering is handled on the server...this is generally working well, but what this also means is that the page is going through many requests/responses in between the time that it is initially loaded and when the user ultimately saves the data.

      Due to the fact that the page is interactive with multiple requests/responses typically taking place, I figured that the action component I'm using to keep track of all the data should be in PAGE scope.
      I set up a page action to execute a method on my action component when the page is navigated which loads the entity I want to edit from the database. It only does this loading once.
      All of this works fine...the show/hide functionality works great (with no javascript thanks to ICEFaces) and the state is tracked properly on the page. However, the problem occurs when I click the save button. The entity manager complains that the entity is detached.

      When I thought about the way Seam handles the EntityManager and transactions, I figure that despite using PAGE scope for my component, I'm getting a new EntityManager on each request and thus when I go to save after editing I'm trying to save to a different EntityManager than the one I loaded the entity from resulting in the detached instance. Is this correct? So if that is correct, what is the best way to deal with this? I tried to mess around with merge, admittedly I'm new to persistence, and from what I've read that should be avoided. Is it bad practice to scope the EntityManager to PAGE scope or is this even possible with Seam controlling transactions and managing the EntityManager?

      PAGE scope seemed correct to me, because all of this work is being done on a single page, but to make it work until I figure out the "right" way, I decided to scope the action component to CONVERSATION, and start a long running conversation on page load and set the EntityManager to manual flush. Everything works fine after that, but I can't help feel its a bit overkill for just editing some data on a single page.
      So is there a best practice that my noobiness has completely missed? Thanks in advance for any help...I'm finding that reading about Seam and using it in a real project are quite difference! =)
        • 1. Re: Need help: Trying to load data from DB, edit it, then save.
          Arbi Sookazian Master

          it depends on how you're injecting the EntityManager instance.  Are you using @In (Seam) or @PersistenceContext (EJB3)?


          You need to be using extended PC (and typically SMPC) to avoid that detachment problem...


          type
          
          public abstract PersistenceContextType type
          
              Specifies whether this is a transaction-scoped persistence context or an extended persistence context.
          
              Default:
                  TRANSACTION
          
          

          • 2. Re: Need help: Trying to load data from DB, edit it, then save.
            Brandon Simpson Newbie
            I'm using @In (Seam-managed). In my work-around version which is conversation scoped, I used <begin-conversation join="true" flush-mode="manual"/> in my pages.xml, but as far as I know it can only be used to extend the PC with conversation scope, not in page scope like I was originally trying to do. It seems like if I'm in page scope and have validation errors or anything else that causes several requests and I have transactions automatically closing the PC on each request I'm going to be getting that detached exception. Is this right?