JDO分页问题

发布于 2024-08-16 08:52:16 字数 2312 浏览 7 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-08-23 08:52:16

抱歉,但

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

不应该这样

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

只是检查。

sorry but

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

should't be

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

?

just checking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文