Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to efficiently retrieve data from DB to populate selectonemenu
    text
    copied!<p>Let's say I have this entity called <code>Customer</code>. <code>Customer</code> objects can be created, deleted or edited in the application.</p> <p>I create a composite component reprensenting a list of <code>Customer</code>, so that I can reuse it in several places in my application.</p> <pre><code>&lt;!-- INTERFACE --&gt; &lt;cc:interface&gt; &lt;cc:attribute name="val" required="true"/&gt; &lt;/cc:interface&gt; &lt;!-- IMPLEMENTATION --&gt; &lt;cc:implementation&gt; &lt;p:selectOneMenu value="val"&gt; &lt;f:selectItems value="#{appManager.customers}" var="cust" itemLabel="#{cust.name}"/&gt; &lt;/p:selectOneMenu&gt; &lt;/cc:implementation&gt; </code></pre> <p>This component is bound to an <code>@ApplicationScope</code> managed bean using an EJB.</p> <pre><code>@Named @ApplicationScoped public class AppManager { @EJB private CustomerFacade customerFacade; public AppManager() { } public List&lt;Customer&gt; customers(){ return customerFacade.findAll(); } } </code></pre> <p>But each time this component will be used, the <code>Customer</code> table will be fetched, am I right? How can I retrive this <code>Customer</code> collection more efficiently? I thought of using lazy-loading of a collection:</p> <pre><code>@Named @ApplicationScoped public class AppManager { @EJB private CustomerFacade customerFacade; private List&lt;Customer&gt; customers; /** * Creates a new instance of AppManager */ public AppManager() { } public List&lt;Customer&gt; getCustomers() { if(customers == null){ customers = customerFacade.findAll(); } return customers; } } </code></pre> <p>But then the changes applied to the DB wouldn't be reflected on the collection.</p> <p>I would greatly appreciate some advice on the common techniques or best practices in this situation. Thanks :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload