Skip to content

Latest commit

 

History

History
45 lines (37 loc) · 2 KB

guide-querydsl-spring.asciidoc

File metadata and controls

45 lines (37 loc) · 2 KB

QueryDSL in devon4j-spring

To implement dynamic queries, devon4j suggests the use of QueryDSL. QueryDSL uses metaclasses generated from entity classes at build time. devon4j-spring provides a way to use QueryDSL without the need for code generation. For this, devon4j provides the interface DefaultRepository that your repository needs to extend and the QueryUtil helper class to build your queries.

Example

Here is an example for using QueryDSL in devon4j-spring:

  public List<DishEntity> findDishes(DishSearchCriteriaTo criteria) {
    DishEntity dish = Alias.alias(DishEntity.class);
    JPAQuery<DishEntity> query = newDslQuery(alias);
    Range<BigDecimal> priceRange = criteria.getPriceRange();
    if (priceRange != null) {
      BigDecimal min = priceRange.getMin();
      if (min != null) {
        query.where(Alias.$(dish.getPrice()).ge(min));
      }
      BigDecimal max = priceRange.getMax();
      if (max != null) {
        query.where(Alias.$(dish.getPrice()).le(max));
      }
    }
    String name = criteria.getName();
    if ((name != null) && (!name.isEmpty())) {
      // query.where(Alias.$(alias.getName()).eq(name));
      QueryUtil.get().whereString(query, Alias.$(alias.getName()), name, criteria.getNameOption());
    }
    return query.fetch();
  }

Pagination

Pagination for dynamic or generally handwritten queries is provided in devon4j-spring via QueryUtil.findPaginated(…​):

boolean determineTotalHitCount = ...;
return QueryUtil.get().findPaginated(criteria.getPageable(), query, determineTotalHitCount);