JPQL中是否有这样的CASE表达式? [英] Is there such thing CASE expression in JPQL?

查看:28
本文介绍了JPQL中是否有这样的CASE表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一张桌子:

TableA:Field1, Field2, Field3

和相关的 JPA 实体类

and associated JPA entity class

@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...
}

有没有什么方法可以构造一条JPQL语句,松散地翻译成这条SQL,即如何将case表达式翻译成JPQL?

Is there any way to construct a JPQL statement that loosely translated to this SQL, ie how to translated the case expression to JPQL?

select field1,
case
  when field2 = 1 then 'One'
  when field2 = 2 then 'Two'
  else 'Other number'
end,
field3
from tableA;

推荐答案

已在 JPA 2.0 中添加

It has been added in 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

这篇关于JPQL中是否有这样的CASE表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆