Java JPA@OneToMany:在查询中仅从map返回一些值
出于性能原因,我正在尝试运行JPA查询,以仅返回实体中的特定字段,而不是整个实体 在该实体内:Java JPA@OneToMany:在查询中仅从map返回一些值,java,jpa,spring-data-jpa,Java,Jpa,Spring Data Jpa,出于性能原因,我正在尝试运行JPA查询,以仅返回实体中的特定字段,而不是整个实体 在该实体内: @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "helper", fetch = FetchType.EAGER) @MapKeyColumn(name = "year") public Map<Integer, DutyHistory> getDutyHistoryList() { return dutyHistoryL
@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();
您应该使用年份参数创建另一个查询
@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);
您应该使用年份参数创建另一个查询
@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);
年份不是Helper对象上的字段,而是DutyHistory对象上的字段。我想知道如何在select语句中将其表示为离散字段,所以我想日期的投影应该在DutyHistory对象上完成。它与您的助手的关系取决于您的模型:join,left join…它将是一个left join。因此,您将有如下内容:从Helper中选择new Helperh.id,h.toto h left join h.dutyHistory d where d.year>=:year@uncleBounty在这种情况下,由于一个助手的关系,作者可以为一个助手获取多个记录。使用诸如“选择新助手”之类的内容可能会更好。。。从Helper h中存在从DutyHistory d中选择d,其中d.Helper=h.helperId和d.year>=:yearYear不是Helper对象上的字段,而是DutyHistory对象上的字段。我想知道如何在select语句中将其表示为离散字段,所以我想日期的投影应该在DutyHistory对象上完成。它与您的助手的关系取决于您的模型:join,left join…它将是一个left join。因此,您将有如下内容:从Helper中选择new Helperh.id,h.toto h left join h.dutyHistory d where d.year>=:year@uncleBounty在这种情况下,由于一个助手的关系,作者可以为一个助手获取多个记录。使用诸如“选择新助手”之类的内容可能会更好。。。从存在的帮助程序h中从DutyHistory d中选择d,其中d.Helper=h.helperId和d.year>=:year