3 Replies Latest reply on Dec 28, 2006 12:39 AM by nbhatia

    Showing updated entity on postback

    nbhatia

      I have created a seam application using seam-gen. I have a page that displays a master entity and its children. The page also allows the user to add a new child. However, when I click the submit button on the page to add the new child, the finally rendered page does not show this child. The reason is that the database is queried BEFORE the child is inserted. See below:

      17:22:47,702 INFO [STDOUT] before - RESTORE_VIEW(1)
      17:22:47,749 INFO [STDOUT] after - RESTORE_VIEW(1)
      17:22:47,765 INFO [STDOUT] before - APPLY_REQUEST_VALUES(2)
      17:22:47,780 INFO [STDOUT] Hibernate: select thread0_.ID as ID161_0_, thread0_.TITLE as TITLE161_0_, thread0_.FORUM_FKas FORUM3_161_0_ from chalktalk.thread thread0_ where thread0_.ID=?
      17:22:47,780 INFO [STDOUT] Hibernate: select posts0_.THREAD_FK as THREAD4_1_, posts0_.ID as ID1_, posts0_.ID as ID160_0_, posts0_.BODY as BODY160_0_, posts0_.THREAD_FK as THREAD4_160_0_, posts0_.DATE_CREATED as DATE3_160_0_ from chalktalk.post posts0_ where posts0_.THREAD_FK=?
      17:22:47,812 INFO [STDOUT] after - APPLY_REQUEST_VALUES(2)
      17:22:47,812 INFO [STDOUT] before - PROCESS_VALIDATIONS(3)
      17:22:47,859 INFO [STDOUT] after - PROCESS_VALIDATIONS(3)
      17:22:47,859 INFO [STDOUT] before - UPDATE_MODEL_VALUES(4)
      17:22:47,905 INFO [STDOUT] after - UPDATE_MODEL_VALUES(4)
      17:22:47,905 INFO [STDOUT] before - INVOKE_APPLICATION(5)
      17:22:48,015 INFO [STDOUT] Hibernate: insert into chalktalk.post (BODY, THREAD_FK, DATE_CREATED, ID) values (?, ?, ?, ?)
      17:22:48,046 INFO [STDOUT] after - INVOKE_APPLICATION(5)
      17:22:48,093 INFO [STDOUT] before - RENDER_RESPONSE(6)
      17:22:48,249 INFO [STDOUT] after - RENDER_RESPONSE(6)
      


      How do I solve this issue/ I would like the newly rendered page to show the inserted child.

      Thanks.

        • 1. Re: Showing updated entity on postback

          Hi,

          I'm not familiar with seam-gen code. But can you provide the rendered page source, as well as your entity + action source.

          It does appear that the new child is added fine, but just not rendering ?

          • 2. Re: Showing updated entity on postback

            Do you add the new the new Post to the Thread? (thread.addPost(newPost), or something to that effect?)

            • 3. Re: Showing updated entity on postback
              nbhatia

              Correct - the child is added fine, but the rendered page does not show the newly added child. The reason is that the Thread and Posts are fetched from the database during the Apply Request Values phase, and only later in the Invoke Application phase the new Post is inserted.

              Here's the source for the rendered page - Thread.xhtml:

              <body>
              <ui:composition template="/layout/template.xhtml">
              
               <ui:define name="body">
              
               <div id="PageSubtitle">Thread</div>
              
               <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
              
               <div id="List">
               <h2>#{threadHome.instance.title}</h2>
              
               <h:outputText value="No posts" rendered="#{empty threadHome.posts}"/>
              
               <div class="Post">
               <ui:repeat value="#{threadHome.posts}" var="post" rendered="#{not empty threadHome.posts}">
               <div class="Body">#{post.body}</div>
               </ui:repeat>
               </div>
               </div>
              
               <h:form>
               <div class="NewPost">
               <div class="Post" >
               <div class="Body">
               <h:inputTextarea id="body"
               rows="4"
               cols="20"
               required="true"
               value="#{postHome.instance.body}"/>
               </div>
               </div>
               </div>
              
               <div class="Info">
               <h:commandButton id="save"
               value="Post Reply"
               action="#{postHome.persist}"
               styleClass="Button" />
               </div>
               </h:form>
              
              
              </ui:define>
              
              </ui:composition>
              </body>
              


              This is ThreadHome:

              @Name("threadHome")
              public class ThreadHome extends EntityHome<Thread> {
              
               @In(value = "#{forumHome.instance}", required = false)
               Forum forum;
              
               public void setThreadId(Long id) {
               setId(id);
               }
              
               public Long getThreadId() {
               return (Long) getId();
               }
              
               @Override
               protected Thread createInstance() {
               Thread result = new Thread();
               result.setForum(forum);
               return result;
               }
              
               public List<Post> getPosts() {
               return getInstance() == null ? null : new ArrayList<Post>(getInstance()
               .getPosts());
               }
              
              }
              


              and finally here's PostHome, which contains the persist action which is called from Thread.xhtml.

              @Name("postHome")
              public class PostHome extends EntityHome<Post> {
              
               @In(value = "#{threadHome.instance}", required = false)
               Thread thread;
              
               public void setPostId(Long id) {
               setId(id);
               }
              
               public Long getPostId() {
               return (Long) getId();
               }
              
               @Override
               protected Post createInstance() {
               Post result = new Post();
               result.setThread(thread);
               return result;
               }
              
               @Override
               public String persist() {
               if (this.instance.getDateCreated() == null) {
               this.instance.setDateCreated(new java.util.Date());
               }
               return super.persist();
               }
              }