2 Replies Latest reply on Mar 29, 2009 10:00 PM by d sailer

    EntityQuery, page context, query caching

    d sailer Newbie

      I'm tweaking a seam-gen crud app and trying to minimize queries from EntityQuery classes used in searches. I came up with my own base class for EntityQuery to help with that:


      public class MyEntityQuery<E> extends EntityQuery<E> {
      
        private List<E> searchResults;
      
        /**
         * getResultList causes the query to fire every time and is the
         * primary reference from the UI so add a
         * special method for that and fire it only once per search
         * 
         * @return
         */
        public void doSearch() { 
          searchResults = super.getResultList();
        }
        
        /**
         * override and just return the results. The doSearch() should already
         * have done the query.
         */
        @Override
        public List<E> getResultList() {
          return searchResults;
        }
      }
      



      this works but I noticed that the constructor of my subclasses to MyEntityQuery would get called 4 times on initial page load and twice each time I hit the search button. So I thought I'd try to put MyEntityQuery in page scope. Now the initial page load calls the constructor 16 times (it appears once per reference from the xhtml file) on initial page load and still calls it twice on initial search (subsequent searches do not call the constructor). I don't think there is anything special about AccountList.xhtml. The search button is


      <h:commandButton id="search" value="Search" action="#{accountList.doSearch}"/>
      



      I'm still trying to get my head wrapped around how the seam scopes work. Can anyone give me some clues on what's happening here?


      thanks

        • 1. Re: EntityQuery, page context, query caching
          Ingo Jobling Master

          Hi,


          Here are a couple of pointers that may help to figure out what is going on:


          1)  The constructor will be called twice each time the component is created, more about this here 


          Suggestion: instead of the constructor, create a method annotated with @Create to get a precise count of component creations.


          2)  The calls to EntityQuery are driven by pages.xml.  More about this here and here


          3)  Note that EntityQuery does not include a @Scope annotation (unlike EntityHome, which is scoped to the conversation).  You might want to add @Scope(CONVERSATION), or use the @Factory annotation.


          Hope this helps.

          • 2. Re: EntityQuery, page context, query caching
            d sailer Newbie

            thanks, that's very helpful. I did add @Scope(PAGE) to EntityQuery. Thats what was causing the 16 calls to the ctor, which I still don't understand. I switched to Scope(CONVERSATION) and get pretty much what I want.