5 Replies Latest reply on Mar 5, 2008 3:24 PM by Ajay Gupta

    Howto detach entity from entitymanager

    P Boevink Newbie

      I have a multipage (wizard like) conversation where the (excisting) entity is changed.
      At the end the user has to explicitly press 'save' to update the entity to the db.
      I've used an entityHome POJO that I've upgraded to a stateful EJB, but every change tot the entity is immediately synchronised with the db.
      I have no option to cancel all changes at the end of my multipage wizard....
      What approach do I need to solve my 'issue'?

        • 2. Re: Howto detach entity from entitymanager
          Nicklas Karlsson Master

          entityManager.clear() detaches everything so it might be overkill. Apparently there is no detach for a single entity in JPA?

          • 3. Re: Howto detach entity from entitymanager
            P Boevink Newbie

            Nicklas Karlsson wrote on Mar 05, 2008 11:59 AM:


            use manual flush mode


            This only affects the flushmode...(the moment when changes are synchronised with the db).
            At conversation end, the changed are still flushed....

            • 4. Re: Howto detach entity from entitymanager
              Nicklas Karlsson Master

              This only affects the flushmode...(the moment when changes are synchronised with the db).
              At conversation end, the changed are still flushed....


              Hmm. I was under the impression that it manual flush mode, anything that isn't explicitly flush()ed gets lost...

              • 5. Re: Howto detach entity from entitymanager
                Ajay Gupta Newbie

                Folks,


                Here are my 25 cents based on my knowledge after working with Seam and EJB3. Hope this helps:


                1. manual-flush mode can only be used if you are using Seam managed persistence contexts (SMPC) and are using Hibernate as the JPA provider. Both of these conditions need to be true for someone to be able to use manual flush mode.


                2. Even with manual flush mode set, your changes won't be automatically flushed until you call em.flush() explicitly (even if the conversation ends and you forgot to do em.flush(), changes will not be flushed and you might end up). However, when you do call modifyEM.flush(), Seam will write all changes made to the persistenc context without having any control over it.


                3. To detach a specific entity, you call em.refresh(entity) which discards all the in-memory changes made on that entity and if you return the newly loaded entity back to the Web Tier, you will have a new detached instance.


                4. em.clear() discards all modified entities in the persistence context. So, only use it when you intend to do like a Reset Screen values functionality in your application.


                Sounds like in your multi-wizard app, em.refresh() is what y ou need to do to implement Reset functionality. Hope this helps.