Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such thing CASE expression in JPQL?

Tags:

java

orm

oracle

jpa

Let say there is a table:

TableA:Field1, Field2, Field3 

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

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; 
like image 351
Nordin Avatar asked Jan 09 '09 09:01

Nordin


People also ask

Is JPQL case sensitive?

Overview. Spring Data JPA queries, by default, are case-sensitive. In other words, the field value comparisons are case-sensitive.

Which of the following are JPQL features?

JPQL FeaturesIt is a platform-independent query language. It is simple and robust. It can be used with any type of database such as MySQL, Oracle. JPQL queries can be declared statically into metadata or can also be dynamically built in code.

Is JPQL simpler than SQL?

JPQL syntax is very similar to the syntax of SQL. Having SQL like syntax is an advantage because SQL is a simple structured query language and many developers are using it in applications. SQL works directly against relational database tables, records and fields, whereas JPQL works with Java classes and instances.

Is JPQL object-oriented?

The Jakarta Persistence Query Language (JPQL; formerly Java Persistence Query Language) is a platform-independent object-oriented query language defined as part of the Jakarta Persistence (JPA; formerly Java Persistence API) specification. JPQL is used to make queries against entities stored in a relational database.


1 Answers

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

like image 161
Sanjeev Kumar Dangi Avatar answered Sep 20 '22 10:09

Sanjeev Kumar Dangi