8 Replies Latest reply on Sep 8, 2008 11:37 AM by skunk

    Query repeated over and over

    skunk

      Hello,


      I have a contact class which is backed by a contact Mysql table. In a facelets view I show a list of these contacts in a datatable:


      <h:outputText value="No contacts to display." rendered="#{empty contactList.contacts}" />
      <h:dataTable id="contactList" var="contact"
                value="#{contactList.contacts}" 
                rendered="#{not empty contactList.contacts}">
           <h:column>
                <f:facet name="header">Id</f:facet>
                #{contact.id}
           </h:column>
           <h:column>
                <f:facet name="header">Name</f:facet>
                <s:link value="#{contact.name}" view="/contacts/contact.xhtml">
                     <f:param name="contactId" value="#{contact.id}" />
                </s:link>
           </h:column>
      </h:dataTable>
      



      contactList is a SLSB with a method getContacts():


      public Collection<?> getContacts() {
           Query q = mudEntityManager.createNamedQuery("Contacts.All");
           return q.getResultList();
      }
      



      The named query is inside my orm.xml:


      <named-query name="Contacts.All">
           <query>
                select contact from Contact contact
           </query>
      </named-query>
      



      When I visit the view, it loads, but 34 identical queries are issued to the database:


      select
          contact0_.contact_id as contact1_7_,
          contact0_.name as name7_,
          contact0_.contact_image_id as contact3_7_ 
      from
          mud.contacts contact0_
      



      Why are so many queries being run? This particular view still loads quickly, but others, where there are difficult queries with thousands of results, take so long to load that transactions time out and the user is redirected to a debug page (after five minutes or so of waiting). If I create a query in my components.xml file, it is executed three times.


      Thanks,
      Dave