找不到能够从 AbstractJPAQuery 转换为 DTO 的转换器
no converter found capable of converting from AbstractJPAQuery to DTO
谁能告诉我如何在 JPA 中使用下面的本机查询?现在我收到错误消息,因为找不到转换器。
Sql 查询在 DB(MYSQL)
中给出正确的响应
SELECT SUM(CASE WHEN Status = 'PENDING' THEN 1 ELSE 0 END) AS `pendingRequest`,
SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' THEN 1 ELSE 0 END) AS `approvedRequest`,
SUM(CASE WHEN Status = 'FAILED' THEN 1 ELSE 0 END) AS `rejectedRequest`,
SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' or Status ='PENDING' or Status ='FAILED' THEN 1 ELSE 0 END) as totalRequest FROM fiu_consent_db.consent_request;
我在 JPA 存储库中使用的查询
@Query(value = "SELECT SUM(CASE WHEN Status = 'PENDING' THEN 1 ELSE 0 END) AS `pendingRequest`,\n" +
" SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' THEN 1 ELSE 0 END) AS `approvedRequest`,\n" +
" SUM(CASE WHEN Status = 'FAILED' THEN 1 ELSE 0 END) AS `rejectedRequest`,\n" +
" SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' or Status ='PENDING' or Status ='FAILED' THEN 1 ELSE 0 END) AS `totalRequest` FROM fiu_consent_db.consent_request",nativeQuery = true)
ConsentRequestCount getAllConsentRequestCount();
我创建的DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConsentRequestCount {
private long pendingRequest;
private long approvedRequest;
private long rejectedRequest;
private long totalRequest;
}
我已经为这 4 个文件创建了一个单独的 DTO,并将数据类型设为 long(也尝试使用 int),但它无法映射。
任何人都可以就此提出建议吗?
根据 this,基于 class 的 DTO 预测不适用于本机查询。您可以通过将 DTO class 更改为接口
来切换到基于接口的 DTO 投影
所以这个 DTO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConsentRequestCount {
private long pendingRequest;
private long approvedRequest;
private long rejectedRequest;
private long totalRequest;
}
改成界面:
public interface ConsentRequestCount {
long getPendingRequest();
long getApprovedRequest();
long getRejectedRequest();
long getTotalRequest();
}
谁能告诉我如何在 JPA 中使用下面的本机查询?现在我收到错误消息,因为找不到转换器。
Sql 查询在 DB(MYSQL)
中给出正确的响应SELECT SUM(CASE WHEN Status = 'PENDING' THEN 1 ELSE 0 END) AS `pendingRequest`,
SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' THEN 1 ELSE 0 END) AS `approvedRequest`,
SUM(CASE WHEN Status = 'FAILED' THEN 1 ELSE 0 END) AS `rejectedRequest`,
SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' or Status ='PENDING' or Status ='FAILED' THEN 1 ELSE 0 END) as totalRequest FROM fiu_consent_db.consent_request;
我在 JPA 存储库中使用的查询
@Query(value = "SELECT SUM(CASE WHEN Status = 'PENDING' THEN 1 ELSE 0 END) AS `pendingRequest`,\n" +
" SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' THEN 1 ELSE 0 END) AS `approvedRequest`,\n" +
" SUM(CASE WHEN Status = 'FAILED' THEN 1 ELSE 0 END) AS `rejectedRequest`,\n" +
" SUM(CASE WHEN Status = 'ACTIVE' or Status ='READY' or Status ='PENDING' or Status ='FAILED' THEN 1 ELSE 0 END) AS `totalRequest` FROM fiu_consent_db.consent_request",nativeQuery = true)
ConsentRequestCount getAllConsentRequestCount();
我创建的DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConsentRequestCount {
private long pendingRequest;
private long approvedRequest;
private long rejectedRequest;
private long totalRequest;
}
我已经为这 4 个文件创建了一个单独的 DTO,并将数据类型设为 long(也尝试使用 int),但它无法映射。
任何人都可以就此提出建议吗?
根据 this,基于 class 的 DTO 预测不适用于本机查询。您可以通过将 DTO class 更改为接口
来切换到基于接口的 DTO 投影所以这个 DTO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConsentRequestCount {
private long pendingRequest;
private long approvedRequest;
private long rejectedRequest;
private long totalRequest;
}
改成界面:
public interface ConsentRequestCount {
long getPendingRequest();
long getApprovedRequest();
long getRejectedRequest();
long getTotalRequest();
}