JPA @OneToMany:在查询中仅从map中返回一些值

时间:2015-12-22 15:54:32

标签: java jpa spring-data-jpa

我正在尝试运行JPA查询以仅返回来自我的实体的特定字段,而不是整个实体(出于性能原因)。

在这个实体中是:

@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "helper", fetch = FetchType.EAGER)
@MapKeyColumn(name = "year")
public Map<Integer, DutyHistory> getDutyHistoryList() {
    return dutyHistoryList;
}

在我的查询中,我想从此地图中返回多个值,例如过去3年来自DutyHistory对象的字段。

我的问题是,这是什么查询语法?我将返回的值映射到POJO,如下所示:

@Query(value = "SELECT new com.castlemon.helpers.dto.ReportHelper(h.helperId, h.firstName, h.secondName"
            + ", h.sex, h.service, h.dateOfBirth, h.schoolGroup, h.orientationRequired, h.notes as adminNotes "
            + ", h.primaryDuty.dutyName as primaryDuty, h.secondDuty, h.secondaryDuty.dutyName as secondaryDuty "            
            + " WHERE h.travelling = 1")
    public List<ReportHelper> getTravellingHelperDetails();

1 个答案:

答案 0 :(得分:0)

您应该使用“年”参数

创建另一个查询
@Query(value = "SELECT new com.castlemon.helpers.dto.ReportHelper(h.helperId, h.firstName, h.secondName"
        + ", h.sex, h.service, h.dateOfBirth, h.schoolGroup, h.orientationRequired, h.notes as adminNotes "
        + ", h.primaryDuty.dutyName as primaryDuty, h.secondDuty, h.secondaryDuty.dutyName as secondaryDuty "            
        + " WHERE h.travelling = 1 AND h.year >= :yearLimit")
public List<ReportHelper> getTravellingHelperDetailsUntilYear(String yearLimit);