select case
when m.age <= 10 then '학생요금'
when m.age >= 60 then '경로요금'
else '일반요금'
end
from Member m```
Code
public class JpaMain {
public static void main(String[] args) {
Member member = new Member();
member.setName("member");
//나이 10살로 설정
member.setAge(10)
em.persist(member);
em.flush();
em.clear();
String query = "select"
+ "case when m.age <= 10 then '학생요금' when m.age >= 60 then '경로요금'"
+ "else '일반요금'"
+ "from Member m";
List<String> result = em.createQuery(query, String.class).getResultList();
for (String s : result) {
System.out.println("s = " + s);
}
tx.commit();
}
}
select case t.name
when '팀A' then '인센티브110%'
when '팀B' then '인센티브120%'
else '인센티브105%'
end
from Team t
select coalesce(m.username, '이름 없는 회원') from Member m
-> m.username
이 null
이면 이름 없는 회원
반환
Code
public class JpaMain {
public static void main(String[] args) {
Member member = new Member();
em.persist(member);
em.flush();
em.clear();
String query = "select coalesce(m.name, '이름 없는 회원')"
+ "from Member m";
List<String> result = em.createQuery(query, String.class).getResultList();
// 이름이 없기 때문에 '이름 없는 회원'으로 출력된다.
for (String s : result) {
System.out.println("s = " + s);
}
tx.commit();
}
}
select NULLIF(m.username, '관리자')from Member m
Code
public class JpaMain {
public static void main(String[] args) {
Member member = new Member();
//이름을 관리자로 설정
member.setName("관리자");
em.persist(member);
em.flush();
em.clear();
//만약 이름이 관리자라면 NULL 출력
String query = "select NULLIF(m.name, '관리자')"
+ "from Member m";
List<String> result = em.createQuery(query, String.class).getResultList();
for (String s : result) {
System.out.println("s = " + s);
}
tx.commit();
}
}
참고 :
김영한. 『자바 ORM 표준 JPA 프로그래밍』. 에이콘, 2015.