假设有一张表:
TableA:Field1, Field2, Field3
和相关的JPA实体类
@Entity @Table(name="TableA") public class TableA{ @Id @Column(name="Field1") private Long id; @Column(name="Field2") private Long field2; @Column(name="Field3") private Long field3; //... more associated getter and setter... }
有没有办法构造一个松散地翻译成这个SQL的JPQL语句,即如何将case表达式翻译成JPQL?
select field1, case when field2 = 1 then 'One' when field2 = 2 then 'Two' else 'Other number' end, field3 from tableA;
Sanjeev Kuma.. 93
它已在JPA 2.0中添加
用法:
SELECT e.name, CASE WHEN (e.salary >= 100000) THEN 1 WHEN (e.salary < 100000) THEN 2 ELSE 0 END FROM Employee e
参考: http ://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0
它已在JPA 2.0中添加
用法:
SELECT e.name, CASE WHEN (e.salary >= 100000) THEN 1 WHEN (e.salary < 100000) THEN 2 ELSE 0 END FROM Employee e
参考: http ://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0