Interface BaseEntityService.MappedQueryBuilder<T>

Type Parameters:
T - The generic base entity type or from a DTO subclass thereof.
Enclosing class:
BaseEntityService<I extends Comparable<I> & Serializable,E extends BaseEntity<I>>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface protected static interface BaseEntityService.MappedQueryBuilder<T>
Functional interface to fine-grain a JPA criteria query for any of BaseEntityService.getPage(Page, boolean) methods taking a specific result type, such as an entity subclass (DTO). You must return a LinkedHashMap with Getter as key and Expression as value. The mapping must be in exactly the same order as constructor arguments of your DTO.

You do not need this interface directly. Just supply a lambda. Below is an usage example:

 public class YourEntityDTO extends YourEntity {

     private BigDecimal totalPrice;

     public YourEntityDTO(Long id, String name, BigDecimal totalPrice) {
         setId(id);
         setName(name);
         this.totalPrice = totalPrice;
     }

     public BigDecimal getTotalPrice() {
         return totalPrice;
     }

 }
 
 @Stateless
 public class YourEntityService extends BaseEntityService<YourEntity> {

     public void getPageOfYourEntityDTO(Page page, boolean count) {
         return getPage(page, count, YourEntityDTO.class (criteriaBuilder, query, root) -> {
             Join<YourEntityDTO, YourChildEntity> child = root.join("child");

             LinkedHashMap<Getter<YourEntityDTO>, Expression<?>> mapping = new LinkedHashMap<>();
             mapping.put(YourEntityDTO::getId, root.get("id"));
             mapping.put(YourEntityDTO::getName, root.get("name"));
             mapping.put(YourEntityDTO::getTotalPrice, builder.sum(child.get("price")));

             return mapping;
         });
     }

 }