2 Replies Latest reply: Apr 27, 2013 7:37 AM by REDO LOG RSS

    problem with JPA EntityManager.createQuery()

    REDO LOG
      Hi all
      I am facing a strange problem with entityManager method createQuery(CriteriaQuery) I got the following error:
       Caused by: java.lang.NullPointerException
           at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.createCompoundQuery(CriteriaQueryImpl.java:543) 
           at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.translate(CriteriaQueryImpl.java:715)
           at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1313)
           at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:500)
           at com.mobiwigo.pay.webportal.subscription.ApplicationManagementService.load(ApplicationManagementService.java:51)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:601)
           at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
           at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
           at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5367)
           at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
           at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
           at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
           at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
           at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      it is reported as a bug but I use the latest EclipseLink api (2.4.x), and the strange thing is when I test the EJB responsible of throwing this exception using an EJB client, everything works fine as expected but integrating this with the web part it just flat away with errors.
      Here is the function overrided load since I use Primefaces API for my presentation layer:

      @Override
           public List<ApplicationDTO> load(
                     int first,
                     int pageSize,
                     String sortedField,
                     com.mobiwigo.common.dataaccess.IDataAccessProxy.SortOrder sortOrder,
                     Map<String, String> filters) {
      
                CriteriaBuilder cb = entityManager.getCriteriaBuilder();
                CriteriaQuery<ApplicationDTO> q = cb.createQuery(ApplicationDTO.class);
                Root<Application> a = q.from(Application.class);
      
                applyFilter(a, cb, q, filters);
                applySort(cb, q, a, sortedField, sortOrder);
      
                q.select(JPAUtils.constructApplicationDTO(cb, a));
      
                return entityManager.createQuery(q).setFirstResult(first)
                          .setMaxResults(pageSize).getResultList();
           }
      the functions applySort and applyFilter are
      private void applySort(CriteriaBuilder cb, CriteriaQuery<ApplicationDTO> q,
                     Root<Application> a, String sortedField, SortOrder sortOrder) {
              if(sortedFiled != null){
                if (sortOrder == SortOrder.ASC) {
      
                     q.orderBy(cb.asc(a.get(sortedField)));
      
                }
      
                if (sortOrder == SortOrder.DESC) {
                     q.orderBy(cb.desc(a.get(sortedField)));
                }
                  }
      
           }
      
           private void applyFilter(Root<Application> a, CriteriaBuilder cb,
                     CriteriaQuery<ApplicationDTO> q, Map<String, String> filters) {
      
              if(filters != null){
      
                List<Predicate> predicates = new ArrayList<Predicate>();
      
                for (String s : filters.keySet()) {
      
                     if (a.get(s) != null) {
      
                          predicates.add(cb.like(a.<String> get(s),
                                    "%" + filters.get(s) + "%"));
                     }
                }
      
                q.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
      
                 }
      
           }