如何使用Spring Data和QueryDSL分页执行JPAQuery [英] How to execute a JPAQuery with pagination using Spring Data and QueryDSL

查看:2135
本文介绍了如何使用Spring Data和QueryDSL分页执行JPAQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 queryDSL 有这个请求:
$ b

  Iterable< ; AO> query_result = new JPAQuery(entityManager).from(ao)
.leftJoin(ao.lots,lot)
.leftJoin(ao.acs,ac)
.where(where).distinct )
.list(ao);

但是如果我们将它与 spring data jpa

  ao_respository.findAll(谓词arg0,Pageable arg1); 

因为我想返回一个 Page 和只有 querydsl 它没有实现页面,而没有 spring data jpa



我尝试将放置在中,其中 Predicate arg0 但我得到这个异常

 未声明的路径'很多'。将此路径作为源查询以便能够引用它

其中 lot 被宣布为 QLot lot = QLot.lot;

解决方案

  JPAQuery query = new JPAQuery() entityManager).from(ao)
.leftJoin(..).fetch()
.leftJoin(..).fetch()
...
.where(where )




MaPage< AO> page = new MaPage< AO>();
page.number = pageNumber + 1;

page.content = query.offset(pageNumber * pageSize).limit(pageSize).list(ao);

page.totalResult = query.count();

My Page class:

  public class MaPage< T> {

public List< T>内容;
public int number;
public Long totalResult;
public Long totalPages;
...
}

它有效,但我得到了这个警告 p>


nov。 21,2014 6:48:54 AM
org.hibernate.hql.internal.ast.QueryTranslatorImpl list WARN:
HHH000104:通过集合提取指定的firstResult / maxResults;
在内存中应用!



I have this request working good with queryDSL :

 Iterable<AO> query_result = new JPAQuery(entityManager).from(ao)
            .leftJoin( ao.lots , lot )
            .leftJoin( ao.acs , ac )
              .where(where).distinct()
                .list(ao);

But what is its equivalent if we use it with spring data jpa

ao_respository.findAll(Predicate arg0, Pageable arg1);

Because i want to return a Page and just with querydsl it doesn't implement Page without spring data jpa.

I try to put my where in Predicate arg0 but i got this exception

Undeclared path 'lot '. Add this path as a source to the query to be able to reference it

where lot is declared as QLot lot = QLot.lot;

解决方案

I created my own Page class and executed the query like this:

    JPAQuery query = new JPAQuery(entityManager).from(ao)               
            .leftJoin( .. ).fetch()
            .leftJoin( .. ).fetch()
            ...
            .where(where)




    MaPage<AO> page = new MaPage<AO>();
    page.number = pageNumber+1;

    page.content = query.offset(pageNumber*pageSize).limit(pageSize).list(ao);

    page.totalResult = query.count();

My Page class:

public class MaPage<T> {

    public List<T> content;
    public int number;
    public Long totalResult;
    public Long totalPages;
    ...
}

It works but I got this warning

nov. 21, 2014 6:48:54 AM org.hibernate.hql.internal.ast.QueryTranslatorImpl list WARN: HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!

这篇关于如何使用Spring Data和QueryDSL分页执行JPAQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆