This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 257026 - Generated JSF implementation very inefficient due to multiple "SELECT COUNT(.." queries for every page view
Summary: Generated JSF implementation very inefficient due to multiple "SELECT COUNT(....
Status: NEW
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 8.1
Hardware: All All
: P3 normal (vote)
Assignee: Sergey Petrov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-12-07 16:58 UTC by rexley
Modified: 2015-12-07 16:58 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rexley 2015-12-07 16:58:35 UTC
"Persistence - JSF Pages from Entity Classes" generates code that issues multiple "SELECT COUNT(.." queries for every page view.  For example, when navigating to the "List" view the "SELCT COUNT(.." query is executed 9 times.  This is incredibly inefficient and makes the UI very sluggish.

One way to remedy this would be to cache the row count in the Abstract Facade, although, there may be better solutions.  When this is the done the UI is 5 to times faster.  Here is an example of how the AbstractFacade could be improved:

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;
    private int cachedCount = -1;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
        cachedCount=-1;
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
        cachedCount=-1;
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        if (cachedCount == -1) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        cachedCount = ((Long) q.getSingleResult()).intValue();
        }
        return cachedCount;
    }