Попытка преобразовать результат из нативного @Query в DTO с использованием проекций, вот код, приведенный ниже. иметь два dto RangeInfo и Ranges Entity - AtmCashPickup и создать собственный запрос в репозитории. Запрос работает нормально. У сущности есть идентификатор в качестве первичного ключа, а у dto нет поля идентификатора. Если я удалю идентификатор из запроса, это не сработает.
public class RangeInfoVO implements Serializable{
public RangeInfoVO(String atm, List<Range> ranges) {
super();
this.atm = atm;
this.ranges = ranges;
}
private String atm;
private List<Range> ranges = new ArrayList<Range>();
public String getAtm() {
return atm;
}
public void setAtm(String atm) {
this.atm = atm;
}
public List<Range> getRanges() {
return ranges;
}
public void setRanges(List<Range> ranges) {
this.ranges = ranges;
}
}
class Range {
public Range(LocalDate start_date, LocalDate end_date) {
this.start_date = start_date;
this.end_date = end_date;
}
private LocalDate start_date;
private LocalDate end_date;
public LocalDate getStart_date() {
return start_date;
}
public void setStart_date(LocalDate start_date) {
this.start_date = start_date;
}
public LocalDate getEnd_date() {
return end_date;
}
public void setEnd_date(LocalDate end_date) {
this.end_date = end_date;
}
}
Уровень сущности ----------------------------- ---
@Table(name = "atm_cash_pickup", uniqueConstraints = {
@UniqueConstraint(columnNames = { "atm_identifier", "expected_pickup" }) })
@Entity
//@Immutable
public class AtmCashPickup implements Serializable{
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
//@Column(name = "id")
private Integer id;
@NotNull
@Column(name = "atm_identifier")
private String atmIdentifier;
@NotNull
@Column(name = "expected_pickup")
private LocalDate expectedPickup;
@Column(name = "actual_pickup")
private LocalDate actualPickup;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAtmIdentifier() {
return atmIdentifier;
}
public void setAtmIdentifier(String atmIdentifier) {
this.atmIdentifier = atmIdentifier;
}
public LocalDate getExpectedPickup() {
return expectedPickup;
}
public void setExpectedPickup(LocalDate expectedPickup) {
this.expectedPickup = expectedPickup;
}
public LocalDate getActualPickup() {
return actualPickup;
}
public void setActualPickup(LocalDate actualPickup) {
this.actualPickup = actualPickup;
}
}
и репозиторий с использованием JPA
public interface CashPickupRepository extends JpaRepository<AtmCashPickup, Integer> {
@Query(
value = "SELECT\r\n" +
" id, atm_identifier,\r\n" +
" MIN(lag_expected_pickup) AS expected_pickup,\r\n" +
" MAX(lead_actual_pickup ) AS actual_pickup\r\n" +
"FROM (\r\n" +
"SELECT\r\n" +
" id, atm_identifier, lag_expected_pickup, lead_actual_pickup, \r\n" +
" CASE WHEN Groups.lag_expected_pickup < lead_actual_pickup THEN 1 ELSE 0 END AS IslandStartInd,\r\n" +
" SUM(CASE WHEN Groups.lag_expected_pickup < lead_actual_pickup THEN 1 ELSE 0 END) OVER (ORDER BY id) AS IslandId\r\n" +
"FROM\r\n" +
"(\r\n" +
" SELECT\r\n" +
" id, atm_identifier, expected_pickup, actual_pickup, \r\n" +
" lag(expected_pickup) ignore nulls over (order by id) as lag_expected_pickup,\r\n" +
" lead(actual_pickup ) over (order by id) as lead_actual_pickup\r\n" +
" FROM ATM_CASH_PICKUP \r\n" +
") Groups where lead_actual_pickup IS NOT NULL\r\n" +
")Islands \r\n" +
"GROUP BY\r\n" +
" id\r\n" +
"ORDER BY \r\n" +
" atm_identifier, actual_pickup",
nativeQuery = true
)
List<RangeInfoVO> getSearchRanges();
}