Search code examples
javajdodatanucleus

Why does executeUnique return forwardQueryResult in DataNucleus?


I have been trying to learn DataNucleus and JDO and found that the following expression, where pm is a PersistenceManager

pm.newQuery(A.class)
  .filter("this.id==:id")
  .setParameters(id)
  .executeUnique();

returns an instance of ForwardQueryResult, which is what is returned by execute methods that return multiple results. However, the signature and description of executeUnique indicates that it should return an instance of the class passed to newQuery. Why is it returning a ForwardQueryResult? Note that the compile-time deduced return type is A, but assigning the expression to a variable of class A results in a ClassCastException


Solution

  • That looks like a bug, since the whole point of executeUnique is to return the candidate/result without having to extract it from a List (which is what that return type you see is). Maybe it is already fixed in their current codebase (and nightly builds), otherwise report it.

    As a workaround, just call setUnique() (the old API way of doing it) on the query?