2012-03-12 3 views
5

다음과 같이 JPA를 사용하여 쿼리를 실행하는 방법은 무엇입니까? WHERE 절에서 CASE WHEN과 함께 JPA 쿼리. 수행하는 방법?

SELECT t 
FROM table t 
WHERE 
    (
    CASE WHEN ((... subquery ...) IS NULL) 
    THEN (t.category IS NULL) 
    ELSE (t.viewId = :viewId) 
    END 
) 

나는 IS

THEN (t.category IS NULL)

는이 가능합니다 MismatchedTokenException에서 얻을 (그것은 일반 SQL로 작동)? 또는이 쿼리를 다시 작성해야합니까?

+0

귀하의 상태를 AND 및 OR을 사용하여 몇 가지 조건으로 나누시겠습니까? –

답변

8

당신은 변환 할 수 있습니다 귀하의 경우처럼 뭔가 절 : 당신은 여기에서 JPA 2.0 스펙 최종 버전을 다운로드 할 수 있습니다

where 
    ((... my first condition ...) and (something is NULL)) 
    or 
    (not (... first condition ...) and (something2 = otherthing)) 
2

나는 그렇게 할 수 없다고 생각합니다. JPQL grammar을 읽으십시오 (여기에 OpenJPA 제공). 다음은 사례 표현식을 정의하는 용어, 특히 사용중인 일반 사례 표현식입니다.

  • case_expression :: = general_case_expression | simple_case_expression | coalesce_expression | nullif_expression
  • general_case_expression :: = CASE는 when_clause {when_clause} * ELSE scalar_expression의 END
  • 는 :: = conditional_expression 다음 scalar_expression는
THEN의 우변의 식 갖는다

scalar_expression 될 WHEN when_clause . 어느 말 :

  • scalar_expression :: = arithmetic_expression | string_primary | enum_primary | datetime_primary | boolean_primary | case_expression | entity_type_expression

나는 모든 용어의 정의를 뿌리로 삼지 않았지만, 그들은 (t.category IS NULL)과 같은 것을 포함하지 않는다고 생각합니다.

관련 문제