JDO分页问题

时间:2009-12-23 04:50:58

标签: java google-app-engine 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;
}

1 个答案:

答案 0 :(得分:0)

抱歉,

String queryString = "select from "+Module.class.getName()+" where owner!='"+owner+"'";

不应该

String queryString = "select from "+Module.class.getName()+" where owner != '"+owner+"'";

只是检查。