JDO 异常:“查询需要 1 个参数,但已提供 2 个值。”
尽管我的 JDO 查询包含两个 declareParameters
语句,但下面的代码生成一个错误,声称只接受一个参数:
查询需要 1 个参数,但已提供 2 个值。
这两个参数是 amountP
和 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);
但是,通过以下更改,它可以工作。
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);
我的问题是:出了什么问题原始语法?
更新:此问题已被其他人报告但没有解释。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JDO API 似乎要求立即设置所有参数。该方法称为declareParameters,它似乎是一个“setter”,而不是一个“adder”。方法名称可能会产生误导,而且文档也不是那么好,但它似乎就是这样。
这与同时支持 setter 和 adder 的“扩展”不同:
addExtension()
、setExtensions()
。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()
.很明显,第二次调用 declareParameters 替换了第一次调用中声明的参数。第二个示例中显示了声明多个参数的正确方法。
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.
解释摘自官方文档:
链接
声明是一个包含一个或多个查询参数的字符串
声明用逗号分隔。中命名的每个参数
当查询时参数声明必须绑定到一个值
被执行。
此方法的 String 参数遵循 Java 语言中形式参数的语法。
The explanation taken from the official documentation:
link here
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.