Java JPQL 中是否有这样的 CASE 表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/427447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:29:26  来源:igfitidea点击:

Is there such thing CASE expression in JPQL?

javaoracleormjpa

提问by Nordin

Let say there is a table:

假设有一张桌子:

TableA:Field1, Field2, Field3

and associated JPA entity class

和相关的 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...
}

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

有没有什么方法可以构建一个松散地转换为这个SQL的JPQL语句,即如何将case表达式转换为JPQL?

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

采纳答案by Sanjeev Kumar Dangi

It has been added in JPA 2.0

它已在 JPA 2.0 中添加

Usage:

用法:

SELECT e.name, CASE WHEN (e.salary >= 100000) THEN 1 WHEN (e.salary < 100000) THEN 2 ELSE 0 END FROM Employee e

Ref:http://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0

参考:http : //en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0

回答by aledbf

You can use the event listeners provided by Jpa to do something when you load one row of the db, ie:

您可以使用 Jpa 提供的事件侦听器在加载一行 db 时执行某些操作,即:

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

    @Transient
    private String field4;

    @PostLoad
    private void onLoad() {
        if (field2 != null) {
            switch (field2.intValue()) {
            case 1:
                field4 = "One";
                break;
            case 2:
                field4 = "Two";
                break;
            default:
                field4 = "Other Number";
                break;
            }
        }
    }
}

(the field4 not persist in the db)

(field4 不存在于数据库中)

(take this like an workaround to "non implemented feature in JPA" like case statements)

(将此作为解决“JPA 中未实现的功能”之类的 case 语句的解决方法)

回答by mgamer

There is certainly such thing in Hibernate so when you use Hibernate as your JPA provider then you can write your query as in this example:

Hibernate 中肯定有这样的东西,所以当你使用 Hibernate 作为你的 JPA 提供者时,你可以像这个例子一样编写你的查询:

    Query query = entityManager.createQuery("UPDATE MNPOperationPrintDocuments o SET o.fileDownloadCount = CASE WHEN o.fileDownloadCount IS NULL THEN 1 ELSE (o.fileDownloadCount + 1) END " +
                                            " WHERE o IN (:operations)");
    query.setParameter("operations", mnpOperationPrintDocumentsList);

    int result = query.executeUpdate();