我有一個很好的帶有 WITH CTE 的 Blaze-Persistence 查詢,與多個其他查詢一起使用,并與聯合系結在一起。查詢具有正確的結果(從 sql 開始)。
public List<FilterResult> getFilters(HourSearchDto hourSearchDto) {
...
FinalSetOperationCriteriaBuilder<FilterResult> cb = cbf.create(entityManager, FilterResult.class)
// Start with the WITH()
.with(HourCTE.class, false)
.from(Hour.class, "hour")
.bind("id").select("id")
.bind("employeeId").select("employee.id")
.bind("taskId").select("task.id")
.where("UPPER(hour.description)").like().expression("'%" hourSearchDto.getDescription().toUpperCase() "%'").noEscape()
.end()
// First select, for reference Employee. Use the selectNew(FilterResult.class) to map the result to FilterResult.
.selectNew(FilterResult.class)
.with("employee.id", "id")
.with("'EMPLOYEE'", "referenceName")
.with("COUNT(hour.employeeId)", "count")
.with("FORMAT('%1$s (%2$s)', employee.firstName, COUNT(hour.employeeId))", "displayValue")
.end()
.from(Employee.class, "employee")
.leftJoinOn(HourCTE.class, "hour")
.on("hour.employeeId").eqExpression("employee.id")
.end()
// UNION to add next select.
.union()
// Next select, for reference Task. Simple select as first select above maps the result to FilterResult already.
.select("task.id", "id")
.select("'TASK'", "referenceName")
.select("COUNT(hour.taskId)", "count")
.select("FORMAT('%1$s (%2$s)', task.name, COUNT(hour.taskId))", "displayValue")
.from(Task.class, "task")
.leftJoinOn(HourCTE.class, "hour")
.on("hour.taskId").eqExpression("task.id")
.end()
.endSet()
.orderByAsc("referenceName")
.orderByAsc("displayValue");
return cb.getQuery().getResultList();
}
問題是,結果是作為 anArrayList<Object[4]>而不是預期的ArrayList<FilterResult>. 當我洗掉聯合及其查詢時,.selectNew(FilterResult.class)正確構造一個ArrayList<FilterResult>.
如何確保包括聯合在內的完整查詢ArrayList<FilterResult>也回傳 an?
為了完整起見:
public class FilterResult {
@Id
Long id;
String referenceName;
Long count;
String displayValue;
public FilterResult() {
}
public FilterResult(Long id, String referenceName, Long count, String displayValue) {
this.id = id;
this.referenceName = referenceName;
this.count = count;
this.displayValue = displayValue;
}
// Getters
}
@CTE
@Entity
public class HourCTE {
@Id
private Long id;
private Long employeeId;
private Long taskId;
}
uj5u.com熱心網友回復:
這是當前 API 的限制。您可以跟蹤https://github.com/Blazebit/blaze-persistence/issues/565了解此事的進展。一種可能的解決方法是將聯合部分包裝到另一個 CTE 中,然后selectNew在從該 CTE 中選擇的查詢中僅使用該部分一次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515511.html
標籤:爪哇休眠火焰持久性
