5 Replies Latest reply on Nov 29, 2009 7:30 PM by schnulla

    rowCount and EntityManager

    schnulla

      Hello,


      I want to see how many users are registered:



      <h:outputText styleClass="outputText" value="#{userList.rowCount} Users"/>





      @Stateless
      @Name("registrationStats")
      public class RegistrationStats implements RegistrationStatsInterface {
           
           @PersistenceContext
           private EntityManager entityManager;
           
           @DataModel     
           private List<User> userList;
           
           @Factory("userList")
           public void populateUserList() {
                userList = entityManager.createQuery("SELECT COUNT(*) FROM User").getResultList();
           }
      }





      @Local
      public interface RegistrationStatsInterface {     
           public void populateUserList();
      }




      But I always get 1 user registered.


      Please can you help me?

        • 1. Re: rowCount and EntityManager
          dhcinc

          In your query, you are asking for a count of the number of users. As such, you will always get one row back from the select.


          Perhaps you meant to use SELECT user from User user?

          • 2. Re: rowCount and EntityManager
            schnulla

            You are right it must be SELECT user from User. Thank you!


            I wonder how I could use COUNT() and outject that value
            to my h:outputText? And how-to update this value because
            at the moment DataModel is only populated on the first
            page visit.


            Thanks for any hints :)


            • 3. Re: rowCount and EntityManager
              schnulla

              You are right it must be SELECT user from User. Thank you!


              uups I meant SELECT name from User

              • 4. Re: rowCount and EntityManager
                meetoblivion

                well if you want to outject the count, you could do something like this:


                private BigDecimal userListSize;

                @Factory("userListSize")
                public void populateUserListSize() {
                userListSize = entityManager.createQuery("SELECT COUNT(*) FROM User").getSingleResult();
                }


                or you could use the existing userList


                private BigDecimal userListSize;

                @Factory("userListSize")
                public void populateUserListSize() {
                userListSize = userList.size();
                }

                • 5. Re: rowCount and EntityManager
                  schnulla

                  Thanks the first works fine and this way I get
                  rid of the @DataModel and .rowCount stuff :)


                  I guess COUNT() is also more performant than
                  SELECT user FROM User?



                  The only thing I have to solve now is to
                  update userListSize on every page visit.
                  What is the standard practice to do this?


                  Thanks for the help :)