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;
Overview. Spring Data JPA queries, by default, are case-sensitive. In other words, the field value comparisons are case-sensitive.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With