JPA-按入学状态表计数(状态类型为Enum)

时间:2018-11-23 12:23:48

标签: java spring-boot jpa enums spring-data-jpa

我已经声明了Enum类型的StatusEnum,例如:

public enum StatusEnum {
PENDING, ACCEPTED, REJECTED, IN_PROGRESS, COMPLETED

}

我正在尝试从状态中获取入场表格的数量,因此我尝试使用查询(在存储库中):

@Query("SELECT COUNT(a.status), a.status FROM AdmissionForms a GROUP BY a.status ORDER BY a.status ASC")
List<Object[]> admissionFormCountByStatus();

然后我将其解析为我在服务类中指定的DTO列表,

List<Object[]> objects = admissionFormRepository.admissionFormCountByStatus();
    for (Object[] singleObject : objects ) {
        admissionFormStatusCountList.add(new AdmissionFormStatusCountResponseDTO(
            ((Number)singleObject[0]).intValue(), (StatusEnum)singleObject[1]));
    }

我的DTO(AdmissionFormStatusCountResponseDTO),例如:

public class AdmissionFormStatusCountResponseDTO {
private int count;
private StatusEnum status;

public AdmissionFormStatusCountResponseDTO(int count, StatusEnum status) {
    this.count = count;
    this.status = status;
  }
}

资源类:

@GetMapping("/workorders/countByStatus")
@Timed
public List<AdmissionFormStatusCountResponseDTO> admissionFormCountByStatus() {
    return admissionFormService.workorderCountByStatus();
}

我得到的结果就像:

[
  {
    "count": 6,
    "status": "ACCEPTED"
  },
  {
    "count": 2,
    "status": "COMPLETED"
  },
  {
    "count": 3,
    "status": "IN_PROGRESS"
  }
]

仅当该状态值存在时,它才使用count进行响应,但对于Enum中尚未分配给任何准入表格的状态,我还要求count = 0。

2 个答案:

答案 0 :(得分:1)

手动准备admissionFormStatusCountList后,在那里缺少哪些枚举,并将其添加为0。您不会从数据库中删除它。

答案 1 :(得分:0)

我做了如下所需的更改。

我将admissionFormStatusCountResponseDTO定义为:

public class AdmissionFormStatusCountResponseDTO {

private Long accepted;
private Long completed;
private Long inProgress;
private Long pending;
private Long rejected;

public AdmissionFormStatusCountResponseDTO() {
}

public AdmissionFormStatusCountResponseDTO(Long accepted, Long completed, Long inProgress, Long pending, Long rejected) {
    this.accepted = accepted;
    this.completed = completed;
    this.inProgress = inProgress;
    this.pending = pending;
    this.rejected = rejected;
  }
//getters and setters
}

然后使用以下DTO更新存储库查询:

@Query("select new com.purvik.app1.service.dto.AdmissionFormStatusCountResponseDTO(" +
    "SUM(CASE WHEN a.status='ACCEPTED' then 1 else 0 END)," +
    "SUM(CASE WHEN a.status='COMPLETED' then 1 else 0 END)," +
    "SUM(CASE WHEN a.status='IN_PROGRESS' then 1 else 0 END)," +
    "SUM(CASE WHEN a.status='PENDING' then 1 else 0 END)," +
    "SUM(CASE WHEN a.status='REJECTED' then 1 else 0 END)" +
    ") from AdmissionForm a")
AdmissionFormStatusCountResponseDTO admissionFormCountByStatusNew();

服务类别:

public AdmissionFormStatusCountResponseDTO admissionFormCountByStatus(){
    log.debug("Request to get count of AdmissionForm by it's Status ");
    return admissionFormRepository.admissionFormCountByStatusNew();
}

资源终结点:

@GetMapping("/admissionForms/countByStatus")
@Timed
public AdmissionFormStatusCountResponseDTO admissionFormCountByStatus() {
    log.debug("get count of status from stored admissionForm");
    return admissionFormService.admissionFormCountByStatus();
}

现在回答:

{
"accepted": 5,
"completed": 0,
"inProgress": 4,
"pending": 1,
"rejected": 1
}

我不需要具有Enum中定义的所有静态变量。这是一个基于构造函数的JPQL查询,可带来预期的结果。希望这对其他人有帮助。

如果您希望在Enum中定义所有静态响应,那么

@Robert Niestroj是正确的。只需编辑有助于我的代码即可。