5

Despite the fact that my JDO query contains TWO declareParameters statements, the code below produces an error claiming only one parameter is accepted:

Query requires 1 parameters, yet 2 values have been provided.

The two parameters are amountP and taxP:

 javax.jdo.Query query= pm.newQuery(Main.class); 
 query.setFilter("amount == amountP && tax < taxP"); 
 query.declareParameters("int amountP"); 
 query.declareParameters("int taxP"); 
 List<Main> results = (List<Main>)query.execute (amountP, taxP); 

However, with the following changes, it works.

 javax.jdo.Query query= pm.newQuery(Main.class); 
 query.setFilter("amount == amountP && tax < taxP"); 
 query.declareParameters("int amountP, int taxP"); 
 List<Main> results = (List<Main>)query.execute (amountP, taxP); 

My question is: What was wrong with the original syntax?

Update: This problem has been reported by others but without explanation.

gustafc
  • 28,465
  • 7
  • 73
  • 99
Uro
  • 183
  • 4
  • 8

3 Answers3

6

The JDO API seems to require that all parameters are set at once. The method is called declareParameters, which seems to be a "setter", and not an "adder". The method name may be misleading, and the documentation is not that great, but it seems to be just the way it is.

This is different from "extensions" that both supports a setter and an adder: addExtension(), setExtensions().

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
2

It seems pretty obvious that the second call to declareParameters replaces the parameter declared in the first call. The correct way to declare more than one parameter is shown in your second example.

Skip Head
  • 7,580
  • 1
  • 30
  • 34
  • 1
    +1 As the behavior does seem to imply that your statement is correct. Perhaps the downvoter would like to explain why he/she downvoted? – jamesmortensen Jan 24 '12 at 19:27
1

The explanation taken from the official documentation: link here

void declareParameters(java.lang.String parameters)
  • Declare the list of parameters query execution. The parameter declaration is a String containing one or more query parameter declarations separated with commas. Each parameter named in the parameter declaration must be bound to a value when the query is executed.

The String parameter to this method follows the syntax for formal parameters in the Java language.

  • Parameters: parameters - the list of parameters separated by commas.
pedro242
  • 33
  • 5