JDO分页问题
我有一个用 26 个实体填充数据存储的单元测试。我有一个 DAO 试图检索这些实体并翻阅它们,但不知何故我无法让它完全工作。如果我执行query.setRange(0,10),它会起作用,但不适用于query.setRange(10,20)。即使单元测试显示我有 26 个实体,查询也会返回 0 个结果。
public void testGetModulesWithPagination(){
String current_user="currentUser";
String tags[] = {"football","soccer"};
for (int i=0;i<=25;i++){
Module m = new Module();
m.setOwner("myName");
m.setObjective("m"+i+" objective");
m.setTitle("m"+i);
m.setTags(tags);
dao.insert(m);
}
Query query = new Query(Module.class.getSimpleName());
List<Module> modules_page_one = dao.getModules(current_user,"football", "1");
List<Module> modules_page_two = dao.getModules(current_user,"football","2");
assertEquals(26, DatastoreServiceFactory.getDatastoreService().prepare(query).countEntities());
assertNotNull(modules_page_one);
assertEquals(10,modules_page_one.size());
assertNotNull(modules_page_two);
assertEquals(10,modules_page_two.size()); //FAILS here
assertFalse(modules_page_one.equals(modules_page_two));
}
这是我的 dao 实现
public List<Module> getModules(String owner,String tag,String page){
getPm();
String queryString = "select from "+Module.class.getName()+" where owner!='"+owner
+"'";
Query query = pm.newQuery(queryString);
Long pageNumber = Long.parseLong(page);
Long lower_bound = pageNumber*10-10;
Long upper_bound = pageNumber*10;
log.info("pageNumber "+pageNumber);
log.info("lower_bound "+lower_bound);
log.info("upper_bound "+upper_bound);
query.setRange(lower_bound, upper_bound);
List<Module> modules = (List<Module>) pm.detachCopyAll((List<Module>) query.execute());
log.info("modules results size "+modules.size());
log.info(modules+"");
List<Module> search_results = new ArrayList<Module>();
for (Module module:modules){
String module_tags[] = module.getTags();
for (int i=0;i<module_tags.length;i++){
if (module_tags[i].equals(tag)){
search_results.add(module);
continue;
}
}
}
closePm();
return search_results;
}
I have a unit test that populates the datastore with 26 entities. I have a DAO that tries to retrieve these entities and page through them but somehow I can't get it to work entirely. If I do query.setRange(0,10) it works but not for query.setRange(10,20). The query returns 0 results even if the unit test shows that I have 26 entities.
public void testGetModulesWithPagination(){
String current_user="currentUser";
String tags[] = {"football","soccer"};
for (int i=0;i<=25;i++){
Module m = new Module();
m.setOwner("myName");
m.setObjective("m"+i+" objective");
m.setTitle("m"+i);
m.setTags(tags);
dao.insert(m);
}
Query query = new Query(Module.class.getSimpleName());
List<Module> modules_page_one = dao.getModules(current_user,"football", "1");
List<Module> modules_page_two = dao.getModules(current_user,"football","2");
assertEquals(26, DatastoreServiceFactory.getDatastoreService().prepare(query).countEntities());
assertNotNull(modules_page_one);
assertEquals(10,modules_page_one.size());
assertNotNull(modules_page_two);
assertEquals(10,modules_page_two.size()); //FAILS here
assertFalse(modules_page_one.equals(modules_page_two));
}
This is my dao implementation
public List<Module> getModules(String owner,String tag,String page){
getPm();
String queryString = "select from "+Module.class.getName()+" where owner!='"+owner
+"'";
Query query = pm.newQuery(queryString);
Long pageNumber = Long.parseLong(page);
Long lower_bound = pageNumber*10-10;
Long upper_bound = pageNumber*10;
log.info("pageNumber "+pageNumber);
log.info("lower_bound "+lower_bound);
log.info("upper_bound "+upper_bound);
query.setRange(lower_bound, upper_bound);
List<Module> modules = (List<Module>) pm.detachCopyAll((List<Module>) query.execute());
log.info("modules results size "+modules.size());
log.info(modules+"");
List<Module> search_results = new ArrayList<Module>();
for (Module module:modules){
String module_tags[] = module.getTags();
for (int i=0;i<module_tags.length;i++){
if (module_tags[i].equals(tag)){
search_results.add(module);
continue;
}
}
}
closePm();
return search_results;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,但
不应该这样
?
只是检查。
sorry but
should't be
?
just checking.