[jboss-user] [JBoss Seam] - Creating several objects in one transaction.

breako do-not-reply at jboss.com
Fri Dec 28 10:08:36 EST 2007


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

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4115929#4115929

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4115929



More information about the jboss-user mailing list