gpt4 book ai didi

java - JPA @OneToMany : Returning only some values from map in a query

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:04 25 4
gpt4 key购买 nike

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

在这个实体中是这样的:

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

我想在我的查询中从该 map 返回多个值,例如过去 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();

最佳答案

您应该使用“年份”参数创建另一个查询

@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);

关于java - JPA @OneToMany : Returning only some values from map in a query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34419689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com