包含聚合和子查询的 JPA 查询的格式?
Format for a JPA query containing an aggregation and subquery?
我有以下实体(为简洁起见省略了一些列):
@Entity
@Table(name = "INSTRUCTION")
public class Instruction {
@Id
@Column(name = "ID", nullable = false, unique = true)
public Long id;
@Column(name = "CURRENT_STATUS", nullable = false)
private InstructionState currentStatus;
@Column(name = "SUBTYPE", nullable = false)
private InstructionAction subtype;
//Getters & Setters
}
我想在 JPA 中编写以下查询来检索按 CURRENT_STATUS
和 SUBTYPE
分组的指令计数。我知道以下 SQL 有效:
SELECT CURRENT_STATUS, SUBTYPE, COUNT(*) count
FROM (SELECT ID, CURRENT_STATUS, SUBTYPE
FROM INSTRUCTION
WHERE VALUE_DATE= '17-JUN-21'
AND LAST_UPDATED >= '16-JUN-21'
AND LAST_UPDATED < '17-JUN-21'
GROUP BY ID, CURRENT_STATUS, SUBTYPE)
GROUP BY CURRENT_STATUS, SUBTYPE;
我想从这个查询中获取结果并将其映射到一个新的对象调用 InstructionCount
:
public class InstructionCount {
private InstructionState status;
private InstructionAction subType;
private Integer count;
public InstructionCount(final InstructionState status, final InstructionAction subType, final Integer count) {
this.status = status;
this.subType = subType;
this.count = count;
}
//Getters and setters
}
问题
我在我的存储库中为此在 JPA 中提出了以下查询 class:
@Query(value = "SELECT new com.modles.InstructionCount(CURRENT_STATUS status, SUBTYPE subType, COUNT(*) count) \n" +
"FROM (SELECT ID, CURRENT_STATUS, SUBTYPE \n" +
"\t\tFROM LTD_RTGS_CASHINSTRUCTION \n" +
"\t\tWHERE VALUE_DATE= :valueDate \n" +
"\t\tAND LAST_UPDATED >= :lastUpdatedFrom \n" +
"\t\tAND LAST_UPDATED < :lastUpdatedTo \n" +
"\t\tGROUP BY ID, CURRENT_STATUS, SUBTYPE)\n" +
"GROUP BY CURRENT_STATUS, SUBTYPE", nativeQuery = true)
List<InstructionCount> findInstructionCounts(@Param("valueDate") LocalDate valueDate, @Param("lastUpdatedFrom") LocalDateTime lastUpdatedFrom, @Param("lastUpdatedTo") LocalDateTime lastUpdatedTo);
问题是这不起作用,我发现这是因为我无法使用 SELECT new com.modles.InstructionCount
和 nativeQuery = true
将其映射到新对象的方法。但是,当我尝试删除 nativeQuery = true
部分和 运行 我的测试时,出现以下错误:
我还注意到 SQL 在 IDE 中出现语法错误,在第二个 SELECT 语句中突出显示,因此我认为在不使用时此格式存在问题nativeQuery.
任何人都可以帮助我解决这个问题吗?我知道这可行,因为当我删除 SELECT new com.modles.InstructionCount
部分时,它只会 return 一个 Object[]
的列表,其中包含我期望的正确值,但我更愿意映射this 到正确的对象作为查询的一部分。或者,如果有一种方法可以使用规范 API 编写此查询,我也会使用该方法!
下面的查询应该可以正常工作,不需要您的 inner/sub 查询
SELECT CURRENT_STATUS, SUBTYPE, COUNT(ID)
FROM INSTRUCTION
WHERE VALUE_DATE= '17-JUN-21'
AND LAST_UPDATED >= '16-JUN-21'
AND LAST_UPDATED < '17-JUN-21'
GROUP BY CURRENT_STATUS, SUBTYPE;
返回结果时应该可以正常工作
指令数
-------->编辑------------>
@Query(value = "SELECT new com.modles.InstructionCount(currentStatus, subtype, count(id)) FROM Instruction WHERE valueDate= :valueDate AND lastUpdatedFrom >= :lastUpdatedFrom AND lastUpdatedTo < :lastUpdatedTo GROUP BY id, currentStatus, subtype")
List<InstructionCount> findInstructionCounts(@Param("valueDate") LocalDate valueDate, @Param("lastUpdatedFrom") LocalDateTime lastUpdatedFrom, @Param("lastUpdatedTo") LocalDateTime lastUpdatedTo);
我有以下实体(为简洁起见省略了一些列):
@Entity
@Table(name = "INSTRUCTION")
public class Instruction {
@Id
@Column(name = "ID", nullable = false, unique = true)
public Long id;
@Column(name = "CURRENT_STATUS", nullable = false)
private InstructionState currentStatus;
@Column(name = "SUBTYPE", nullable = false)
private InstructionAction subtype;
//Getters & Setters
}
我想在 JPA 中编写以下查询来检索按 CURRENT_STATUS
和 SUBTYPE
分组的指令计数。我知道以下 SQL 有效:
SELECT CURRENT_STATUS, SUBTYPE, COUNT(*) count
FROM (SELECT ID, CURRENT_STATUS, SUBTYPE
FROM INSTRUCTION
WHERE VALUE_DATE= '17-JUN-21'
AND LAST_UPDATED >= '16-JUN-21'
AND LAST_UPDATED < '17-JUN-21'
GROUP BY ID, CURRENT_STATUS, SUBTYPE)
GROUP BY CURRENT_STATUS, SUBTYPE;
我想从这个查询中获取结果并将其映射到一个新的对象调用 InstructionCount
:
public class InstructionCount {
private InstructionState status;
private InstructionAction subType;
private Integer count;
public InstructionCount(final InstructionState status, final InstructionAction subType, final Integer count) {
this.status = status;
this.subType = subType;
this.count = count;
}
//Getters and setters
}
问题
我在我的存储库中为此在 JPA 中提出了以下查询 class:
@Query(value = "SELECT new com.modles.InstructionCount(CURRENT_STATUS status, SUBTYPE subType, COUNT(*) count) \n" +
"FROM (SELECT ID, CURRENT_STATUS, SUBTYPE \n" +
"\t\tFROM LTD_RTGS_CASHINSTRUCTION \n" +
"\t\tWHERE VALUE_DATE= :valueDate \n" +
"\t\tAND LAST_UPDATED >= :lastUpdatedFrom \n" +
"\t\tAND LAST_UPDATED < :lastUpdatedTo \n" +
"\t\tGROUP BY ID, CURRENT_STATUS, SUBTYPE)\n" +
"GROUP BY CURRENT_STATUS, SUBTYPE", nativeQuery = true)
List<InstructionCount> findInstructionCounts(@Param("valueDate") LocalDate valueDate, @Param("lastUpdatedFrom") LocalDateTime lastUpdatedFrom, @Param("lastUpdatedTo") LocalDateTime lastUpdatedTo);
问题是这不起作用,我发现这是因为我无法使用 SELECT new com.modles.InstructionCount
和 nativeQuery = true
将其映射到新对象的方法。但是,当我尝试删除 nativeQuery = true
部分和 运行 我的测试时,出现以下错误:
我还注意到 SQL 在 IDE 中出现语法错误,在第二个 SELECT 语句中突出显示,因此我认为在不使用时此格式存在问题nativeQuery.
任何人都可以帮助我解决这个问题吗?我知道这可行,因为当我删除 SELECT new com.modles.InstructionCount
部分时,它只会 return 一个 Object[]
的列表,其中包含我期望的正确值,但我更愿意映射this 到正确的对象作为查询的一部分。或者,如果有一种方法可以使用规范 API 编写此查询,我也会使用该方法!
下面的查询应该可以正常工作,不需要您的 inner/sub 查询
SELECT CURRENT_STATUS, SUBTYPE, COUNT(ID)
FROM INSTRUCTION
WHERE VALUE_DATE= '17-JUN-21'
AND LAST_UPDATED >= '16-JUN-21'
AND LAST_UPDATED < '17-JUN-21'
GROUP BY CURRENT_STATUS, SUBTYPE;
返回结果时应该可以正常工作
指令数
-------->编辑------------>
@Query(value = "SELECT new com.modles.InstructionCount(currentStatus, subtype, count(id)) FROM Instruction WHERE valueDate= :valueDate AND lastUpdatedFrom >= :lastUpdatedFrom AND lastUpdatedTo < :lastUpdatedTo GROUP BY id, currentStatus, subtype")
List<InstructionCount> findInstructionCounts(@Param("valueDate") LocalDate valueDate, @Param("lastUpdatedFrom") LocalDateTime lastUpdatedFrom, @Param("lastUpdatedTo") LocalDateTime lastUpdatedTo);