2015-12-22 19 views
1

我試圖運行JPA查詢以僅從我的實體返回特定字段,而不是整個實體(出於性能原因)。JPA @OneToMany:在查詢中只返回地圖中的一些值

在這個實體是這樣的:

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

我想,我的查詢範圍內,例如,從本地圖返回多個值來自DutyHistory對象的最近3年的領域。

我的問題是,這是什麼查詢語法?我映射返回值的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(); 

回答

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); 
+0

一年不輔助對象的一個​​字段,它是在DutyHistory對象。我想知道如何在select語句中將其表示爲離散字段。 – TrueDub

+0

所以我猜日期的投影應該在DutyHistory對象上完成。它與您的助手的關係取決於您的模型:加入,左連接... – uncleBounty

+0

這將是左連接。 – TrueDub