Rudy
Rudy

Reputation: 749

Returning Page<Object> in Query DSL

I am using Query DSL and want my result set to return a Page Object. Is there a way to do that in Query DSL? If so Whats my query going to be looking like? I am using JPAQuery and I have my QClasses

The Method structure is this

public Page<Object> searchPerson(String name,String phone){
 Page<Object> results=null;
 JPQLQuery query = new JPAQuery(entityManager);
 QPerson person = QPerson.person;
   //I am assuming my query would go here 
  results = query.from(person). ?????  


return results;} 

Help!

Upvotes: 2

Views: 5575

Answers (2)

Rudy
Rudy

Reputation: 749

The solution for the above was using BooleanBuilder implemented on the method above and changed the method name to return Person Object. Please check BooleanBuilder

QPerson person= QPerson.person;
BooleanBuilder builder = this.getBuilder(name, phone,page, pageSize, sortFlag, sortItem);
    PageRequest pg = getPRequest(page, pageSize);
    Page<Person> pages personRepo.findAll(builder,pg);
  return pages;

and then Implemented getBuilder Method for it which is the below one

public BooleanBuilder getBuilder(String name, String phone, Integer page, Integer pageSize, String sortFlag, String sortItem) {

    QPerson person = QPerson.person;
    BooleanBuilder builder = new BooleanBuilder();
    builder.and(person.name.startsWith(name));
    return builder;
}

and finally implemented the getPRequest Method as the following

public PageRequest getPRequest(Integer page, Integer pageSize) {
    return new PageRequest(page, pageSize);
}

Oooooh Happy Days!

Upvotes: 0

Meiko Rachimow
Meiko Rachimow

Reputation: 4724

Here is my implementation for Paging with QueryDSL. A PageRequest defines the parameters for our query (limit and page):

public class PageRequest {
    protected Long page = 1l;// 1 is the first page
    protected Integer limit = 10;

    public PageRequest(Long page, Integer limit) {
        this.limit = limit;
        this.page = page;   
    }

    public Long getPage() {
        return page;
    }

    public Integer getLimit() {
        return limit;
    }

    public Long getOffset() {
        return (page - 1l) * limit;
    }
}

The Page Class contains the result (here the attribute objects) of the query and could implement methods to create nice paging links.

public class Page<T> extends PageRequest {

    protected Collection<T> objects;
    private Long totalCount;
    private Long pageCount;
    private Boolean hasPageLinkPrev;
    private Boolean hasPageLinkNext;
    private Collection<Long> pageLinks;

    public Page(Long page, Integer limit, Long totalCount, Collection<T> objects) {

        this.page = page;
        this.limit = limit;
        this.totalCount = totalCount;
        this.objects = objects;

        this.pageCount = totalCount / limit;
        if (totalCount % limit > 0) {
            this.pageCount = this.pageCount + 1;
        }

        this.hasPageLinkPrev = page > 1;
        this.hasPageLinkNext = page < this.pageCount;

        this.pageLinks = new ArrayList<>();
        if (this.pageCount != 1) {
            this.pageLinks.add(1l);
            if (page > 3l) {
                this.pageLinks.add(-1l);
            }
            if (page > 2l) {
                if (page.equals(this.pageCount) && this.pageCount > 3l) {
                    this.pageLinks.add(page - 2l);
                }
                this.pageLinks.add(page - 1l);
            }
            if (page != 1l && !page.equals(this.pageCount)) {
                this.pageLinks.add(page);
            }
            if (page < this.pageCount - 1l) {
                this.pageLinks.add(page + 1l);

                if (page == 1l && this.pageCount > 3l) {
                    this.pageLinks.add(page + 2l);
                }
            }
            if (page < this.pageCount - 2l) {
                this.pageLinks.add(-1l);
            }
            this.pageLinks.add(this.pageCount);
        }
    }

    public Page(PageRequest pageRequest, Long totalCount, Collection<T> objects) {
        this(pageRequest.getPage(), pageRequest.getLimit(), totalCount, objects);
    }

    public Long getTotalCount() {
        return this.totalCount;
    }

    public Long getPageCount() {
        return this.pageCount;
    }

    public Long getPage() {
        return this.page;
    }

    public Integer getLimit() {
        return this.limit;
    }

    public Boolean getHasPageLinkPrev() {
        return this.hasPageLinkPrev;
    }

    public Boolean getHasPageLinkNext() {
        return hasPageLinkNext;
    }

    public Collection<Long> getPageLinks() {
        return pageLinks;
    }

    public Collection<T> getObjects() {
        return objects;
    }
}

With that stuf it is not very hard to create the query and put the results in our page object. One possibility is to write a generic method in the base class of the repository classes:

    protected <T> Page<T> getPage(JPQLQuery<T> query, PageRequest pageRequest) {

        List<T> resultList = query
            .offset(pageRequest.getOffset())
            .limit(pageRequest.getLimit())
            .fetch();

        Long totalCount = query.fetchCount();
        return new Page<T>(pageRequest, totalCount, resultList);
    }

In your repository class you create your query for the particular use case. Then you can use the method getPage to get the results in a Page.

public Page<Person> searchPerson(String name,
                                 String phone,
                                 PageRequest request){
  Page<Person> results=null;
  JPQLQuery<Person> query = new JPAQuery<>(entityManager);
  QPerson person = QPerson.person;
  query = query.from(person)
     .where(person.name.eq(name)
         .and(person.phone.eq(phone)));
  return getPage(query, request);
} 

Upvotes: 2

Related Questions