来自 CriteriaBuilder 的 EclipseLink NullPointerException

Posted

技术标签:

【中文标题】来自 CriteriaBuilder 的 EclipseLink NullPointerException【英文标题】:EclipseLink NullPointerException from CriteriaBuilder 【发布时间】:2014-09-23 07:39:08 【问题描述】:

我正在使用 PrimeFaces 数据表通过 LazyDataModel 从我的数据库中加载数据。这一直运作良好。但是最近我在稍微更改代码时遇到了一个奇怪的问题。我认为这与 JSF/PrimeFaces 无关。

问题是每当我在getFilterCondition() 方法中使用filterCondition = cb.or(filterCondition, cb.like(path, value)) 时,我都会在底部显示异常。但是,每当我使用and() 而不是or() 时,一切都会完美运行。

另外。我已经在 Eclipse 中以调试模式运行它,并且 Eclipse 找不到任何为空的变量或值。

这是代码:

@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】:

首先,您使用的是 EclipseLink。不是 OpenJPA。

第二次 NPE 总是一个错误,我建议你 open a report.

【讨论】:

这绝对是一个错误。是的,我正在使用 EclipseLink,我的错。谢谢!

以上是关于来自 CriteriaBuilder 的 EclipseLink NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

CriteriaBuilder - 倒置 LIKE

如何通过两个字段加入(CriteriaBuilder)

集合的 CriteriaBuilder 和 CriteriaQuery 之间的区别

如何使 CriteriaBuilder 加入自定义“开启”条件?

实体列表的 CriteriaBuilder 和 isMember (Hibernate)

两列之间带有 Like 的 CriteriaBuilder 查询