CriteriaBuilder中的EclipseLink NullPointerException

时间:2014-09-23 07:39:09

标签: primefaces eclipselink jpa-2.1

我使用PrimeFaces数据表通过LazyDataModel从我的数据库加载数据。这一直很完美。但最近我遇到了一个奇怪的问题,当我稍微改变了代码。我认为这与JSF / PrimeFaces无关。

问题在于,每当我在filterCondition = cb.or(filterCondition, cb.like(path, value))方法中使用getFilterCondition()时,我都会在底部显示异常。但是,每当我使用and()代替or()时,每次操作都会完美无缺。

另外。我在Eclipse中以调试模式运行它,eclipse找不到任何变量或值为null。

这是代码:

@Override
public List<Application> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
    List<Application> data = new ArrayList<Application>();
    try {
        dao.beginTransaction();
        data = getResultList(first, pageSize, sortField, sortOrder, getFilters());
        setRowCount(count(getFilters()));
        dao.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}

private Predicate getFilterCondition(CriteriaBuilder cb, Root<Application> myObj, Map<String, Object> filters) {
        Predicate filterCondition = cb.conjunction();
        String wildCard = "%";
        for (Entry<String, Object> filter : filters.entrySet()) {
            String value = wildCard + filter.getValue() + wildCard;
            if(filter.getValue().getClass().equals(String.class)) {
                if (!filter.getValue().equals("")) {
                    Path<String> path = getPath(cb, filter.getKey(), myObj);
                    filterCondition = cb.or(filterCondition, cb.like(path, value));
                    // filterCondition = cb.and(filterCondition, cb.like(path, value)); <--- Working
                } 
            } 
        }
        return filterCondition;
    }

    public int count(Map<String, Object> filters) {
        CriteriaBuilder cb = dao.getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Long> cq = cb.createQuery(Long.class);
        Root<Application> myObj = cq.from(Application.class);
        cq.where(getFilterCondition(cb, myObj, filters));
        cq.select(cb.count(myObj));
        return dao.getEntityManager().createQuery(cq).getSingleResult().intValue();
    }

    public List<Application> getResultList(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        CriteriaBuilder cb = dao.getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Application> cq = cb.createQuery(Application.class);
        Root<Application> myObj = cq.from(Application.class);
        q.where(getFilterCondition(cb, myObj, filters));
        if (sortField != null) {
            Path<?> path = getPath(cb, sortField, myObj);
            if (sortOrder == SortOrder.ASCENDING) {
                cq.orderBy(cb.asc(path));
            } else {
                cq.orderBy(cb.desc(path));
            }
        }
        return dao.getEntityManager().createQuery(cq).setFirstResult(first).setMaxResults(pageSize).getResultList();
    }

    private Path<String> getPath(CriteriaBuilder cb, String field, Root<Application> app) {
        Path<String> path = null;
        if (field == null) {
            path = app.get(Application_.appName);
        } else {
            if (field.equals("appName")) {
                path = app.get(Application_.appName);
            } else if (field.equals("contextRoot")) {
                path = app.get(Application_.contextRoot);
            } else if (field.equals("moduleMappings")) {
                path = app.get(Application_.moduleMappings);
            } else if (field.equals("virtualHost")) {
                path = app.get(Application_.virtualHost);
            } else if (field.equals("vhAliases")) {
                path = app.get(Application_.vhAliases);
            } else if (field.equals("server.serverName")) {
                path = app.join("server", JoinType.LEFT).get("serverName");
            } else if (field.equals("cluster.clusterName")) {
                path = app.join("cluster", JoinType.LEFT).get("clusterName");
            } else if (field.equals("cell.cellName")) {
                path = app.join("cell", JoinType.LEFT).get("cellName");
            } else if (field.equals("cell.stage")) {
                path = app.join("cell", JoinType.LEFT).get("stage");
            } else if (field.equals("cell.zone")) {
                path = app.join("cell", JoinType.LEFT).get("zone");
            } else if (field.equals("cell.cellVersion")) {
                path = app.join("cell", JoinType.LEFT).get("cellVersion");
            }
        }
        return path;
    }

抛出异常:

[2014-09-23 09:34:09:782 CEST] 0000009f SystemErr     R java.lang.NullPointerException
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.eclipse.persistence.internal.jpa.querydef.CriteriaBuilderImpl.or(CriteriaBuilderImpl.java:413)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at net.wasreport.models.ApplicationLazyModel.getFilterCondition(ApplicationLazyModel.java:99)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at net.wasreport.models.ApplicationLazyModel.getResultList(ApplicationLazyModel.java:125)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at net.wasreport.models.ApplicationLazyModel.load(ApplicationLazyModel.java:75)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:818)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.component.datatable.DataTableRenderer.preRender(DataTableRenderer.java:94)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:82)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:535)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at javax.faces.component.UIData.encodeEnd(UIData.java:867)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.primefaces.component.outputpanel.OutputPanelRenderer.encodeEnd(OutputPanelRenderer.java:37)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:535)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:626)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.apache.myfaces.context.servlet.PartialViewContextImpl$PhaseAwareVisitCallback.processRenderComponent(PartialViewContextImpl.java:559)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.apache.myfaces.context.servlet.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:537)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at org.apache.myfaces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:207)
[2014-09-23 09:34:09:783 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.visitTree(UIComponent.java:773)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1007)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIForm.visitTree(UIForm.java:269)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.visitTree(UIComponent.java:793)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1007)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.visitTree(UIComponent.java:793)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1007)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.visitTree(UIComponent.java:793)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1007)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.visitTree(UIComponent.java:793)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1007)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:426)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:346)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:390)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:617)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1320)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:240)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1227)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:776)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:458)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[2014-09-23 09:34:09:784 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:909)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[2014-09-23 09:34:09:785 CEST] 0000009f SystemErr     R     at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1862)

1 个答案:

答案 0 :(得分:0)

首先,您正在使用EclipseLink。不是OpenJPA。

其次,NPE始终是一个错误,我建议你open a report.