1

谁能告诉我为什么下面的代码在最后一行导致错误,命令“执行”和错误消息:

Query 类型中的方法 execute(Object, Object, Object) 不适用于参数(Long、Long、Date、Date)

Query q = pm.newQuery(Appointment.class,"AdminID == AID"+
  " && EmployeeID == CEID"+
  " && Time > STime"+
  " && Time < ETime");
q.declareImports("import java.util.Date");
q.declareParameters("Long AID, Long CEID, Date STime, Date ETime");
q.setOrdering("Time");
Appointments = (List<Appointment>) q.execute(AdminID, CurrentEmployeeID, Time1, Time2);

据我所知(由错误消息暗示,执行函数最多只能进行 3 次争论,如果是这种情况,谁能建议如何实现我想要做的事情?我尝试了以下代码,但每次运行时都会出现解析错误!

Query q = pm.newQuery(Appointment.class,"AdminID == "+AdminID+
  " && EmployeeID == "+CurrentEmployeeID+
  " && Time > "+Time1+
  " && Time < "+Time2);
q.declareImports("import java.util.Date");
q.setOrdering("Time");
Appointments = (List<Appointment>) q.execute();

我得到的解析错误是:

org.datanucleus.store.query.QueryCompilerSyntaxException:无法解析表达式的一部分:BST 2012 年 8 月 13 日 11:44:55 && 时间 < Mon Aug 13 11:45:05 BST 2012

4

1 回答 1

3

尝试executeWithArrayexecuteWithMap

HashMap<String, Object> params = new HashMap<String, Object>();
params.put( "AID", adminId );
params.put( "CEID", currentEmployeeId );
params.put( "STime", startTime );
params.put( "ETime", endTime );

query.executeWithMap( params );

笔记:

  1. 变量应该是 lowerCamelCase
  2. startTime并且endTime比 更具描述Time1Time2
  3. “Id”通常比“ID”更受欢迎 - id 的正确 Java 命名约定是什么?
  4. 您的第二次尝试将不起作用,因为它隐式调用..." && Time > " + Time1.toString() + ...
于 2012-08-12T00:41:54.573 回答