2 Replies Latest reply on Dec 31, 2007 7:19 AM by breako

    Creating several objects in one transaction.

    breako

      Hi,
      I have a simple XHTML page which is a form for a Person object.
      I have a create button which will persists the Person and a commit button which commits the EntityManager by calling the appropriate methods on a Seam backing bean.

      My backing bean has a Person object reference which the XHTML form refers to.

      XHTML form...

      
      <table>
      
       <tr>
       <td>Your name:</td>
       <td>
       <s:decorate>
       <h:inputText value="#{person.name}" size="15"/>
       </s:decorate>
       </td>
       </tr>
      
       <tr>
       <td>Your age:</td>
       <td>
       <s:decorate>
       <h:inputText value="#{person.age}" size="15"/>
       </s:decorate>
       </td>
       </tr>
      
       <tr>
       <td>Email:</td>
       <td>
       <s:decorate>
       <h:inputText value="#{person.email}" size="15"/>
       </s:decorate>
       </td>
       </tr>
      
       <tr>
       <td>Comment:</td>
       <td>
       <s:decorate>
       <h:inputTextarea value="#{person.comment}"/>
       </s:decorate>
       </td>
       </tr>
      
      </table>
      
      ...
      
      <h:commandButton type="submit" value="Persist"
       action="#{manager.persist}"/>
      
      <h:commandButton type="submit" value="Commit"
       action="#{manager.commit}"/>
      
      


      I want to create many persons in the one form. so I want something like this to happen

      1. User enters person data
      2. User hits persist
      3. Person is persisted
      4. Form is cleared, User enters data for second person
      5. User hits persist
      6. Person 2 is persisted
      7. User hits commit
      8. Person1 and Person2 are committed.

      Right now I can't do this. the same Person object is always used so Person2 simple wips Person1 and only one Person gets sent to the DB.

      Fair enough, as I have only one Person object in backing bean.
      I was thinking of using a List but then how does the FORM know which element to refer to?

      The only way I can think of doing this is by using a separate Person reference in the backing bean. So that when persist() is invoked, the current Person is copied and then persisted such as this:

      
       @Begin(join=true, flushMode=FlushModeType.MANUAL)
       public void createPerson() {
      
       Person tempPerson = new Person();
       tempPerson.setId(person.getId());
       tempPerson.setName(person.getName());
       tempPerson.setAge(person.getAge());
       tempPerson.setEmail(person.getEmail());
       tempPerson.setComment(person.getComment());
       em.persist(tempPerson);
      }
      
       @End
       public String commit () {
       em.flush();
       }
      


      I think I am doing some of unnecessary work here and I was wondering would someone have a smarter idea?

      Thanks

        • 1. Re: Creating several objects in one transaction.
          evdelst

          Easiest is to add the persons to a list you store in the bean.

          eg.

          private Person person;
          
          private List<Person> list=new ArrayList<Person>();
          
          void add() {
           list.add(person);
           person = new Person();
          )


          Then in your commit method, iterate the list and call em.persist for each element.
          No need to use Flushmode.manual for this kind of interaction.
          Also, the transaction is tied to a single method call (= a good thing).



          • 2. Re: Creating several objects in one transaction.
            breako

             

            "evdelst" wrote:
            Easiest is to add the persons to a list you store in the bean.

            eg.

            private Person person;
            
            private List<Person> list=new ArrayList<Person>();
            
            void add() {
             list.add(person);
             person = new Person();
            )


            Then in your commit method, iterate the list and call em.persist for each element.
            No need to use Flushmode.manual for this kind of interaction.
            Also, the transaction is tied to a single method call (= a good thing).


            Thanks for that, food for thought.