title: JPA query with SELECT CASE WHEN
body: <p>Have a query like this, </p>
  
  <pre><code>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; 
  </code></pre>
  
  <p>which I need to write this in JPA.</p>
  
  <pre><code>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";
  </code></pre>
  
  <p>Tried this way which throws exception like "<strong>Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: = near line 1, column 260 [SELECT............."</strong></p>
  
  <p>Cant we write the case queries in JPA ?</p>
  
  <p>Refereed this link <a href="https://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0</a> which says yes.</p>