Projections - griffio/griffio.github.io GitHub Wiki

Projections

@QueryProjection

com.mysema.query.annotations

Can be used for the View Model, within the JPA environment it can provide a detached model, or DTO layer.


List<PresentableSalary> projection = CollQueryFactory
    .from(QEmployeeSalary.employeeSalary, employeeSalaries)
    .list(new QPresentableSalary(QEmployeeSalary.employeeSalary.employeeRef, QEmployeeSalary.employeeSalary.payDate, QEmployeeSalary.employeeSalary.salaryDetails));

public class PresentableSalary implements Serializable {
 
    private final Long employeeRef;
    private final List<SalaryDetail> salaryDetails;
    private final LocalDate payDate;
  
    @QueryProjection
    public PresentableSalary(Long employeeRef, LocalDate payDate, List<SalaryDetail> salaryDetails) {
        this.employeeRef = employeeRef;
        this.payDate = payDate;
    	this.salaryDetails = salaryDetails;
    }
 
    public List<SalaryDetail> salaryDetails() {
        return salaryDetails;
    }
 
    public LocalDate getPayDate() {
 	return payDate;
    }

    public Long employeeRef() {
      	return this.employeeRef;	
    }

}

MappingProjection - Optionally support construction of several different projections

com.mysema.query.types

public class PresentableSalaryProjection extends MappingProjection<PresentableSalary> {
 
    public PresentableSalaryProjection() {
        super(PresentableSalary.class, employee.employeeRef, payroll.payDate, salary.salaryDetails);
    }
 
    @Override
    protected PresentableSalary map(Tuple row) {
        return new PresentableSalary(row.get(employee.employeeRef), row.get(payroll.payDate), row.get(salary.salaryDetails));
    }
 
}

An @QueryProjection can also be placed on the Entity constructor itself and, in this example, is generated as QSalaryDetail.create().

@QueryProjection 
public SalaryDetail(String salaryName, BigDecimal salary) {
   this.salaryName = salaryName;
   this.salary = salary;
}