4 Replies Latest reply on Jun 18, 2006 2:15 AM by javajedi

    Can't get database updated without calling flush()

      I'm writing my first Seam component to update a persistent entity. Here's the code:

      @Stateless
      @Scope(ScopeType.EVENT)
      @Name("accountEditor")
      public class AccountEditorBean implements AccountEditor {
      
       @In(required=false)
       private AccountManager accountManager;
      
       @Valid
       private Account account;
      
       @In(create=true)
       private Session session;
      
       public String select() {
       account = accountManager.getSelectedAccountSummary().getAccount();
       return "editAccount";
       }
      
       @IfInvalid(outcome=Outcome.REDISPLAY, refreshEntities=true)
       public String update() {
       session.update(account);
       // NOTE: Without this line uncommented, no database update occurs!
       //session.flush();
       return "home";
       }
      
       public Account getAccount() {
       return account;
       }
      }


      Note the commented-out line. When I have the flush() commented out, no database updates are happening to account. When I manually flush in the update() method, the database updates occur as I would expect. Surely, it's not intended that I have to manually flush every time I want to update something. Am I just missing something painfully obvious?

      I am using Seam 1.0, JBoss 4.0.4, and Hibernate 3.2cr1. I am using the SeamExtendedManagedPersistencePhaseListener.

      Any suggestions would be appreciated.

        • 1. Re: Can't get database updated without calling flush()
          gavin.king

          So you have:

          <property name="transaction.flush_before_completion">true</property>


          in hibernate.cfg.xml?

          (Along with all the other settings used in the example app?)

          • 2. Re: Can't get database updated without calling flush()

            Yes.

            • 3. Re: Can't get database updated without calling flush()
              gavin.king

              Turn on Hibernate debug logging and check if the afterCompletion phase is running at all.

              • 4. Re: Can't get database updated without calling flush()

                I don't see anything in the logs about afterCompletion. However, I modified my EJB to be stateful with conversation scope, and now the database updates seem to be working. Here is the modified code:

                @Stateful
                @Scope(ScopeType.CONVERSATION)
                @Name("accountEditor")
                public class AccountEditorBean implements AccountEditor {
                
                 @In(required=false)
                 private AccountManager accountManager;
                
                 @Valid
                 private Account account;
                
                 @In(create=true)
                 private Session session;
                
                 @Begin(join=true)
                 public String select() {
                 account = accountManager.getSelectedAccountSummary().getAccount();
                 return "editAccount";
                 }
                
                 @End
                 @IfInvalid(outcome=Outcome.REDISPLAY, refreshEntities=true)
                 public String update() {
                 session.update(account);
                 return "home";
                 }
                
                 public Account getAccount() {
                 return account;
                 }
                
                 @Remove
                 @Destroy
                 public void destroy() {}
                
                }
                


                I'm still trying to wrap my head around Seam, so I'm not quite sure why this bean works, but the other (stateless) bean doesn't. If you could offer any other suggestions or explanations, I would appreciate it. Otherwise, at least I seem to have found a working pattern.