在JPA存储库Rest资源中执行本机查询

时间:2017-08-12 18:01:26

标签: java spring hibernate rest spring-data-jpa

我遇到了将带有两个参数的自定义查询传递给Spring my Spring RepositoryRestResource接口列表的问题:

    @RepositoryRestResource(collectionResourceRel = "benefitplan", path = "benefitplan")
public interface BenefitPlanRepository extends PagingAndSortingRepository<BenefitPlan, Long> {
    BenefitPlan findTop1ByBenefitPackageCode(@Param("bpcode") String benefitPackageCode);
    BenefitPlan findByBenefitPackageCode(@Param("bpcode") String benefitPackageCode);
    BenefitPlan findTop1ByBenefitPackageCodeAndPlanStatus(@Param("bpcode") String benefitPackageCode, @Param("status") String status);
    @Query(value = 
            "SELECT b.BENEFIT_PLAN_ID FROM BENEFIT_PLAN b left join MEDICAL_PLAN m on b.MEDICAL_PLAN_ID = m.MEDICAL_PLAN_ID "
            + "left join RX_PLAN r on b.RX_PLAN_ID = r.RX_PLAN_ID WHERE r.MTV_RX_CD = ?2 and m.MTV_MEDICAL_CD = ?1"
            , nativeQuery = true)
    BenefitPlan findByMedicalPlanIdAndRxPlanId(@Param("medicalPlanId") String medicalPlanId, @Param("rxPlanId") String rxPlanId);
}

它的最后一个查询,findByMedicalPlanIdAndPlanId给了我一个问题。其余的工作正常。我的错误是:

  

出现意外错误(type = Internal Server Error,   状态= 500)。无法执行查询;嵌套异常是   org.hibernate.exception.GenericJDBCException:无法执行查询

1 个答案:

答案 0 :(得分:0)

尝试

@Query(value = 
            "SELECT b.BENEFIT_PLAN_ID FROM BENEFIT_PLAN b left join MEDICAL_PLAN m on b.MEDICAL_PLAN_ID = m.MEDICAL_PLAN_ID "
            + "left join RX_PLAN r on b.RX_PLAN_ID = r.RX_PLAN_ID WHERE r.MTV_RX_CD = :rxPlanId and m.MTV_MEDICAL_CD = :medicalPlanId"
            , nativeQuery = true)
    BenefitPlan findByMedicalPlanIdAndRxPlanId(@Param("medicalPlanId") String medicalPlanId, @Param("rxPlanId") String rxPlanId);

如果您使用@Param,您希望使用命名参数,您应该在查询中执行此操作。如果它不起作用,我建议你在db client中执行查询以确保它有效。