Spring MVC 如何在Sping Boot 中使用@Query注解引用JPQL中的外键变量?

af7jpaap  于 2023-10-24  发布在  Spring
关注(0)|答案(1)|浏览(140)

这是我的仓库界面中的查询。

@Query("SELECT new com.wellsfargo.loanManagementSystem.model.LoanCardAndEmpCardProjection"
        + "(l.loanId,l.loanType,l.durationInYears," 
        + "e.cardIssueDate)"
        + "FROM LoanCardMaster l JOIN l.loanEmpCardDetails e where e.employeeId = :employeeId")

这就是我如何在下面祈祷的。

List<LoanCardAndEmpCardProjection> findSelectedFieldsFromLoanAndEmpCards(@Param("employeeId") String employeeId);

emploeeId是loanEmpCardDetails的外键(emploeeCardDetails表)。我在查询或下面的函数中引用emploeeId不正确,因为当我在WorkBench中作为查询执行时,我得到了正确的结果。

select
l1_0.loan_id,l1_0.loan_type,l1_0.duration_in_years,l2_0.card_issue_date
from loan_card_master l1_0 join employee_card_details l2_0 on
l1_0.loan_id=l2_0.loan_id_loan_id where
l2_0.employee_id_employee_id=1;

LoanCardAndEmpCardProjection模型类

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class LoanCardAndEmpCardProjection {
    
    private String loanId;
    private String loanType;
    private Integer durationInYears;
    private LocalDate cardIssueDate;
}

这是LoanCardController,当我执行时,我得到状态500,内部服务器错误。

@GetMapping("/getEmpLoanData/{id}")
public ResponseEntity<List<LoanCardAndEmpCardProjection>> getLoanInfo(@PathVariable String  id) {
    try {
        List<LoanCardAndEmpCardProjection> selectedFields = loanCardService.getLoanInfo(id);
        return ResponseEntity.ok(selectedFields);
    } catch (Exception e) {
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}

另外,当我在Postman中运行GET请求而不带@PathVariable注解时,我在控制台中得到了这个值,所以我假设没有正确传递MykeId。

Hibernate: select l1_0.loan_id,l1_0.loan_type,l1_0.duration_in_years,l2_0.card_issue_date from loan_card_master l1_0 join employee_card_details l2_0 on l1_0.loan_id=l2_0.loan_id_loan_id where l2_0.employee_id_employee_id=?

任何帮助都是感激不尽的。

dced5bon

dced5bon1#

看起来id是Long类型的,所以尝试更改它

List<LoanCardAndEmpCardProjection> findSelectedFieldsFromLoanAndEmpCards(@Param("employeeId") Long employeeId);

相关问题