Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 340280 - NullPointerException in CriteriaQueryImpl when using multiselect
Summary: NullPointerException in CriteriaQueryImpl when using multiselect
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P2 major (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-17 04:04 EDT by Lucas Persson CLA
Modified: 2022-06-09 10:34 EDT (History)
2 users (show)

See Also:


Attachments
Proposed patch (2.98 KB, patch)
2011-08-15 16:48 EDT, David Minsky CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Lucas Persson CLA 2011-03-17 04:04:22 EDT
Hi

I get a NullPointerException when caling EntityManager.createQuery(criteria)
at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryI mpl.createCompoundQuery(CriteriaQueryImpl.java:543)
(I am using EclipseLink 2.1.2.v20101206-r8635 from within Weblogic 10.3.4, I have also tried EclipseLink 2.3.0.v20110202-r8914)

Here is the code:
public List<Tuple> getTopUsage( int lenght) {
CriteriaBuilder builder = mEntityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

SingularAttribute<Cdr, String> attribute = Cdr_.callingParty;
Root<Cdr> cdr = criteria.from(Cdr.class);

criteria.multiselect(cdr.get(attribute), builder.count(cdr.get(attribute)));
criteria.where(builder.conjunction());
criteria.groupBy(cdr.get(attribute));

TypedQuery<Tuple> query = mEntityManager.createQuery(criteria);
query.setMaxResults(lenght);
List<Tuple> list = query.getResultList();
return list;
}

Checking the code in EclipseLink then it (createCompoundQuery) assumes that the criteriaQuery's where member always has a "current node" and that is not the case with a predicate like the one I can get from the builder like this: builder.conjunction().

(in my real code I pass in some parameters to build up the 'where' clause)

Here is the stacktrace.

java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryI mpl.createCompoundQuery(CriteriaQueryImpl.java:543)
at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryI mpl.translate(CriteriaQueryImpl.java:715)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.creat eQuery(EntityManagerImpl.java:1313)
at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at weblogic.persistence.QueryProxyImpl.instantiateQuery(QueryPr oxyImpl.java:56)
at weblogic.persistence.QueryProxyImpl.createQuery(QueryProxyIm pl.java:74)
at weblogic.persistence.QueryProxyImpl.getResultList(QueryProxy Impl.java:145)
at xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.CdrBeanImpl.getTopUsage(CdrBea nImpl.java:230)


Thanks
Lucas
Comment 1 Tom Ware CLA 2011-04-06 08:28:08 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 2 David Minsky CLA 2011-08-15 16:40:41 EDT
Reproduced with the following test code:

    public void testTupleQuery() {
        EntityManager em = createEntityManager();
        
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
        
        Root<Employee> emp = criteria.from(Employee.class);
        Path exp = emp.get("lastName");

        criteria.multiselect(exp, qb.count(exp));
        criteria.where(qb.conjunction());
        criteria.groupBy(exp);

        TypedQuery<Tuple> query = em.createQuery(criteria);
        
        List<Tuple> list = query.getResultList();
    }

Stacktrace:

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:1412)
	at org.eclipse.persistence.testing.tests.jpa.criteria.JUnitCriteriaSimpleTestSuite.testTupleQuery(JUnitCriteriaSimpleTestSuite.java:2852)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at org.eclipse.persistence.testing.framework.junit.JUnitTestCase.runBare(JUnitTestCase.java:551)
Comment 3 David Minsky CLA 2011-08-15 16:48:26 EDT
Created attachment 201525 [details]
Proposed patch
Comment 4 David Minsky CLA 2011-08-17 14:11:13 EDT
Fixed in trunk (2.4) at revision: 9900
Comment 5 David Minsky CLA 2011-08-17 16:03:14 EDT
Fixed in 2.3 branch (2.3.1) at revision: 9902
Comment 6 David Minsky CLA 2011-08-17 16:57:44 EDT
Fixed in 2.2 branch at revision: 9905
Comment 7 David Minsky CLA 2011-08-17 18:00:53 EDT
Fixed in 2.1 branch at revision: 9906
Comment 8 Eclipse Webmaster CLA 2022-06-09 10:34:44 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink