2 Replies Latest reply on Dec 7, 2007 6:54 AM by Bartosz Jamroz

    Why do I need to use entityManager.flush() ?

    Bartosz Jamroz Newbie

      Why do I need to use flush() when using SMPC ?

      Before I had this

      @Stateful
      @Scope(ScopeType.APPLICATION)
      @Name("questionTypeManager")
      public class QuestionTypeManagerBean implements QuestionTypeManager {
      
       @Logger
       Log log;
      
       @PersistenceContext
       EntityManager entityManager;
      
       public void addQuestionType(String questionType) {
       entityManager.persist(new QuestionType(questionType));
       log.info("Successfuly persisted question type");
       }
      
       @Remove @Destroy
       public void destroy() {}
      
      }
      


      But when I`ve switched to SMPC I had to add entityManager.flush() couse entityManager didn`t persist QuestionType to the database. It didn`t show any errors either.

      Why is that ?

        • 1. Re: Why do I need to use entityManager.flush() ?
          Pete Muir Master

          Post your SMPC version of the code.

          • 2. Re: Why do I need to use entityManager.flush() ?
            Bartosz Jamroz Newbie

             

            @Stateful
            @Scope(ScopeType.APPLICATION)
            @Name("questionTypeManager")
            public class QuestionTypeManagerBean implements QuestionTypeManager {
            
             @Logger
             Log log;
            
             @In
             EntityManager entityManager;
            
             public void addQuestionType(String questionType) {
             entityManager.persist(new QuestionType(questionType));
             entityManager.flush();
             log.info("Successfuly persisted question type");
             }
            
             public QuestionType getQuestionTypeByName(String questionTypeName) {
             return (QuestionType)entityManager.createQuery("from QuestionType q where q.questionType = '" + questionTypeName + "'").getSingleResult();
             }
            
             public List<QuestionType> getAllQuestionTypes() {
             return entityManager.createQuery("from QuestionType qt").getResultList();
             }
            
             @Remove @Destroy
             public void destroy() {}
            
            }