2 Replies Latest reply on Jul 27, 2010 4:20 AM by vestnik

    EntityController.persist + Redirect problems

    vestnik

      I have several pages for creating entities. They works in the way: user fills a form and perss create button. Action for this button is a method in a class derived from EntityController. This class is not SEAM component it is just a property of conversation scoped comopnent. Everything lives in long running conversation. I have code like this in my create method:


      Entity entity = new Entity();
      ...
      // filling values from form
      ...
      persist(entity);
      
      Redirect redirect = Redirect.instance();
      redirect.setViewId("/entity/view.xhtml");
      redirect.setParameter("id",entity.getId());
      redirect.setConversationPropagationEnabled(false);
      redirect.execute();



      I have one create page where new entity is not saved in the database. The only difference between this view and others is additional ajax requests during editing form (there is one selectOneMenu which possible options are recalculated depending on the value of one rich:calendar field on changing this date).


      I can see in the server log that hibernate requests new value of the sequence (I'm using PostgreSQL) and I can see SELECT request executed from initialization of the view page but there is no INSERT statement between them. If I remove redirect code entity is inserted into my database.


      I've tried to call getEntityManager().joinTransaction(); in the beginning of my method. After that entity is saved but it looks like transaction is commited after redirect. EntityManager.find(Entity.class, id) returns null when I initialize model for the page I'm redirecting to. If I navigate to this page again new entity is loaded correctly.


      Can somebody give an advise how to fix this problem?

        • 1. Re: EntityController.persist + Redirect problems
          thokuest
          entityManager.persist(entity);
          entityManager.flush(); // force INSERT



          Hope that helps!

          • 2. Re: EntityController.persist + Redirect problems
            vestnik

            Thanks for your answer. When I start play with flush I found that on the line redirect.setParameter(id,entity.getId()); I'm actually using wrong entity instance and id parameter is set to 0. After fixing it this code works correctly:


            getEntityManager().joinTransaction();
            Entity entity = new Entity();
            ...
            // filling values from form
            ...
            persist(entity);
            
            Redirect redirect = Redirect.instance();
            redirect.setViewId("/entity/view.xhtml");
            redirect.setParameter("id",entity.getId());
            redirect.setConversationPropagationEnabled(false);
            redirect.execute();