DataNucleus JIRA is now in read-only mode. Raise any new issues in GitHub against the plugin that it applies to. DataNucleus JIRA will remain for the foreseeable future but will eventually be discontinued
Issue Details (XML | Word | Printable)

Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Julian Schillinger
Votes: 0
Watchers: 1

If you were logged in you would be able to see more operations.
DataNucleus Store RDBMS

Null Pointer exception when running executeWithArray together with addGroup

Created: 07/Jan/14 12:32 PM   Updated: 11/Jan/14 07:43 PM   Resolved: 10/Jan/14 09:02 AM
Component/s: Queries
Affects Version/s: None
Fix Version/s: 3.2.11

File Attachments: 1. Java Source File (13 kB)
2. Zip Archive (25 kB)

Environment: Ubuntu 13.10 64bit, OpenJDK 7

Datastore: MySQL
Severity: Production

 Description  « Hide
*** Overview:
When defining a custom/named fetch group in JDO files and running query.executeWithArray a Null Pointer Exception is thrown. Testcase attached.

*** Stacktrace
...19:20:30,428 DEBUG [DataNucleus.Datastore.Native] - SELECT 'org.datanucleus.test.Parent' AS NUCLEUS_TYPE,`A0`.`KEY`,`A0`.`VERSION` FROM `PARENT` `A0` WHERE `A0`.`KEY` = <'161'>
19:20:30,428 DEBUG [DataNucleus.Datastore.Native] - SELECT 'org.datanucleus.test.Parent' AS NUCLEUS_TYPE,`A0`.`KEY`,`A0`.`VERSION` FROM `PARENT` `A0` WHERE `A0`.`KEY` = <'161'>
19:20:30,429 DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 1 ms
19:20:30,429 DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 1 ms
19:20:30,431 DEBUG [DataNucleus.Datastore.Retrieve] - >> JDOQL Bulk-Fetch of org.datanucleus.test.Parent.children
19:20:30,431 DEBUG [DataNucleus.Datastore.Retrieve] - >> JDOQL Bulk-Fetch of org.datanucleus.test.Parent.children
19:20:30,432 ERROR [DataNucleus.General] - >> Error reading objects
at java.util.HashMap.putAll(
at org.datanucleus.api.jdo.JDOQuery.executeWithArray(
at org.datanucleus.test.Main.main(
19:20:30,433 INFO [DataNucleus.General] - >> Stop

*** Relevant code piece

// set up fetch group
pm1.getFetchPlan().addGroup("fetchgrp1"); // adding this line causes null pointer further down

// getting parent object"Getting parent object with key "
+ keyToLookFor);
Query query = pm1.newQuery(Parent.class);
query.setFilter("key == my_key");
query.declareParameters("String my_key");
Object[] params = { keyToLookFor };
List<Parent> parents = (List<Parent>) query
.executeWithArray(params); // this line throws the exception
Parent parent = parents.get(0);

*** Libs

Sort Order: Ascending order - Click to sort in descending order
Julian Schillinger added a comment - 07/Jan/14 02:22 PM
Think this is related to the 'new' bulk fetching of nested objects if they are in the fetch group in combination with the use of explicit parameters (ie the declareParameter() call) in the JDOQL query.

Julian Schillinger added a comment - 08/Jan/14 07:28 AM
This occurs when calling q.execute, q.executeWithArray and q.executeWithMap.

It appears that SQLStatementHelper.applyParametersToStatement (line 164) checks whether parameter paramNameByPosition (which is fed from datastoreCompilation.getParameterNameByPosition()) is null, but BulkFetchHelper.applyParametersToStatement does not do so (line 214).

Andy Jefferson added a comment - 08/Jan/14 12:08 PM
Suggest you work with the raiser of NUCRDBMS-748 and develop a fix

Julian Schillinger added a comment - 08/Jan/14 05:51 PM
Hi Andy,

Am willing to help. I would just catch the null pointer and basically not add the parameter in BulkFetchHelper.applyParametersToStatement. Unfortunately I do not fully comprehend the intention behind this function and why it is invoked. Can you shed some more light on this?


Julian Schillinger added a comment - 10/Jan/14 04:23 AM - edited
Hi Andy,

Below code change in BulkFetchHelper makes my test pass (updated file attached). As mentioned I am not totally confident that this fix does not break something else. Please consider it or give me a pointer in case I missed something.

public void applyParametersToStatement(PreparedStatement ps, RDBMSQueryCompilation datastoreCompilation, SQLStatement sqlStmt, Map parameters)
     // fixes to resolve NUCRDBMS-747
     Map<Integer,String> parameterNameByPos = datastoreCompilation.getParameterNameByPosition();
     if (parameterNameByPos==null) {
     parameterNameByPos=new HashMap<Integer,String>();
     // previous code where datastoreCompilation.getParameterNameByPosition() is replaced by parameterNameByPos
        Map<Integer, String> stmtParamNameByPosition = null;
        List<SQLStatementParameter> stmtParams = null;
        if (datastoreCompilation.getStatementParameters() != null)
            stmtParams = new ArrayList<SQLStatementParameter>();
            stmtParamNameByPosition = new HashMap<Integer, String>();
            int numUnions = sqlStmt.getNumberOfUnions();
            int numParams = stmtParamNameByPosition.size();
            for (int i=0;i<numUnions;i++)
                Iterator<Map.Entry<Integer, String>> paramEntryIter = parameterNameByPos.entrySet().iterator();
                while (paramEntryIter.hasNext())
                    Map.Entry<Integer, String> paramEntry =;
                    stmtParamNameByPosition.put(numParams*(i+1) + paramEntry.getKey(), paramEntry.getValue());

            SQLStatementHelper.applyParametersToStatement(ps, query.getExecutionContext(), stmtParams, stmtParamNameByPosition, parameters);

Andy Jefferson added a comment - 10/Jan/14 09:02 AM
Thx for your code proposal. GitHub master has a slightly different variant, but with the same effect.

PS, please make any code contributions in "diff" format or as a Git pull request. Thx