framework:entity-query : Query is invoked multiple times
kipod Oct 7, 2009 12:21 PMHi all,
I'm trying to avoid multiple queries into the DB, my design is obviously wrong somehow, though I don't know how should this be elegantly acompilshed.
I have a Client entity and a Sale entity, where each Sale has a Client.
from Sale.java :
private Client client; @NotNull @ManyToOne @JoinColumn(name="client_id") public Client getClient() { return client; } public void setClient(Client client) { this.client = client; }
The Client entity has no awareness of any Sales (it's a one-way relation thing).
I have this in my components.xml :
<framework:entity-query name="allClientsQuery" ejbql="select c from Client c" order="c.name asc" /> <factory name="allClients" value="#{allClientsQuery.resultList}" scope="conversation"/>
And in the Sales.xhtml page I have a search box where you can select a Client as a search criteria:
<h:selectOneMenu value="#{saleList.clientId}"> <s:selectItems value="#{allClients}" var="c" itemValue="#{c.id}" label="#{c.name}" /> </h:selectOneMenu>
As well as a table that shows the current search results, one column on which is the Client of the Sale's name:
<rich:dataTable id="saleTable" onRowMouseOver="this.style.backgroundColor='#F8F8F8'" onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'" value="#{saleList.resultList}" var="_sale" sortMode="single" rows="10" rendered="#{not empty saleList.resultList}"> ... <rich:column sortBy="#{_sale.client.name}" width="150px" > <f:facet name="header"> <h:outputText value="#{messages['client.client']}" /> </f:facet> <h:outputText id="saleClient" value="#{_sale.client.name}" /> </rich:column> ... </rich:dataTable>
The saleList
component is a simple EntityQuery<Sale> (the MyList class is a common base class for lists, doesn't add much) :
@Name("saleList") @Scope(ScopeType.CONVERSATION) public class SaleList extends MyList<Sale> { private static final String EJBQL = "select s from Sale s"; ... RESTRICTIONS and search criteria stuff ... }
AND - When the page is displayed the allClientsQuery
(select c from Client c asc ...
)
is ran as well as a
"select client0_.id as id0_0_, ... client0_.name as name0_0_, ... from Client client0_ where client0_.id=?"
For each one of the Sales.
How can I make it so that a single query into the Clients table answers all the page's needs ??
Thanks!