使用SELECT CASE WHEN进行JPA查询

时间:2017-07-05 09:15:58

标签: spring-mvc jpa spring-data-jpa

有这样的查询,

select *
from job_profile
where case
    when exists (
        select 1
        from job_profile
        where screening_type_id = 2 and job_category_id = 4 
        )
    then screening_type_id = 2 and job_category_id = 4 
    else screening_type_id =4
    end; 

我需要在JPA中写这个。

String GET_JOB_VACCINATIONS_BY_JOB_CAT_ID_AND_SCRN_TYPE_ID = "SELECT jobvacc FROM JobVaccination jobvacc WHERE  CASE"
            + " WHEN EXISTS (  SELECT 1 FROM  JobVaccination jobvacc2 WHERE  jobvacc2.jobCategoryMast.jobCategoryId=:jobCategoryId AND jobvacc2.screeningTypeMast.screeningTypeId=:screeningTypeId )"
            + " THEN  jobvacc.jobCategoryMast.jobCategoryId=4 AND jobvacc.screeningTypeMast.screeningTypeId=2 "
            + " ELSE jobvacc.screeningTypeMast.screeningTypeId=2"
            + " END";

尝试这种抛出异常的方式,如“引起:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:=靠近第1行,第260列[SELECT ......... ......“

我们不能在JPA中编写案例查询吗?

参考此链接https://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0,说“是”。

0 个答案:

没有答案