Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql EclipseLink(JPA2.1)查询没有返回明确应该返回的结果_Mysql_Jpa_Eclipselink - Fatal编程技术网

Mysql EclipseLink(JPA2.1)查询没有返回明确应该返回的结果

Mysql EclipseLink(JPA2.1)查询没有返回明确应该返回的结果,mysql,jpa,eclipselink,Mysql,Jpa,Eclipselink,我正在尝试使用下面的代码从数据库检索下一页的结果 public Collection<Product> getCategoryProducts(Category selectedCategory, String start) { Query query = em.createQuery("SELECT p FROM Product p WHERE p.categoryId = :categoryId") .setParameter

我正在尝试使用下面的代码从数据库检索下一页的结果

   public Collection<Product> getCategoryProducts(Category selectedCategory, String start) {
        Query query = em.createQuery("SELECT p FROM Product p WHERE p.categoryId = :categoryId")
                .setParameter("categoryId", selectedCategory)
                .setMaxResults(20);
        if (null != start) {
            query.setFirstResult(Integer.parseInt(start));
        }
        return query.getResultList();
    }
公共集合getCategoryProducts(类别选择类别,字符串开始){
Query Query=em.createQuery(“从产品p中选择p,其中p.categoryId=:categoryId”)
.setParameter(“categoryId”,selectedCategory)
.setMaxResults(20);
如果(null!=开始){
setFirstResult(Integer.parseInt(start));
}
返回query.getResultList();
}
我的产品表大约有1000行,其中每个产品都属于一个类别。 如果我选择一个类别,比如类别1,它有80种产品。我能够查看4页的产品,每页20种产品。当我尝试访问属于更高类别的产品时,问题就出现了。我只能查看前20个结果,但下一页返回0个结果。 例如,类别15的产品范围从
id=459到id=794,其中id是主键

当我从
https://localhost:8181/myapp/category?categoryId=15
但单击底部的“更多”链接
https://localhost:8181/myapp/category?categoryId=15&start=478
返回0个结果。我错过了什么


另请参见这个问题

我终于找到了我的错误。显然,我为第一个结果设置的位置应该是
页码和页面大小的乘积。在我的例子中,我将第一个结果设置为我正在检索的实体的
Id
。因此,对于前几页,结果大小包含
ID
,但随着页码的增加,
ID
不包含在结果集中。这是一种错误,你不知道,你不知道,直到你知道,你不知道

public Collection<Product> getCategoryProducts(Category selectedCategory, String page) {
    int pageSize = 20;
    EntityManager entityManager = Persistence.createEntityManagerFactory("techbayPU").createEntityManager();
    Query query = entityManager.createQuery("SELECT p FROM Product p WHERE p.categoryId = :categoryId ORDER BY p.id", Product.class);
    query.setMaxResults(pageSize);
    if (null != page) {
        query.setFirstResult(Integer.parseInt(page) * pageSize);
    }
    query.setParameter("categoryId", selectedCategory);
    return query.getResultList();
}
公共集合getCategoryProducts(类别选择类别,字符串页){
int pageSize=20;
EntityManager EntityManager=Persistence.createEntityManager工厂(“techbayPU”).createEntityManager();
Query Query=entityManager.createQuery(“从产品p中选择p,其中p.categoryId=:categoryId按p.id排序”,Product.class);
query.setMaxResults(页面大小);
如果(空!=第页){
query.setFirstResult(整数.parseInt(第页)*pageSize);
}
query.setParameter(“categoryId”,selectedCategory);
返回query.getResultList();
}