Skip to content

Commit 1a5cee7

Browse files
committed
HHH-13095 - Document how to use arithmetic expressions in CASE statements
1 parent f4e36a1 commit 1a5cee7

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

documentation/src/main/asciidoc/userguide/chapters/query/hql/HQL.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,27 @@ include::{sourcedir}/HQLTest.java[tags=hql-searched-case-expressions-example]
15471547
----
15481548
====
15491549

1550+
[[hql-case-arithmetic-expressions]]
1551+
=== CASE expressions with arithmetic operations
1552+
1553+
If you want to use arithmetic operations in the CASE expressions, you need to wrap the arithmetic operation in parentheses
1554+
as illustrated by the following example:
1555+
1556+
[[hql-case-arithmetic-expressions-example]]
1557+
.Case expression with arithmetic operation example
1558+
====
1559+
[source, JAVA, indent=0]
1560+
----
1561+
include::{sourcedir}/HQLTest.java[tags=hql-case-arithmetic-expressions-example]
1562+
----
1563+
====
1564+
1565+
[IMPORTANT]
1566+
====
1567+
Without wrapping the arithmetic expression in `(` and `)`, the entity query parser will not be able to
1568+
parse the arithmetic operators.
1569+
====
1570+
15501571
[[hql-nullif]]
15511572
=== NULLIF expressions
15521573

documentation/src/test/java/org/hibernate/userguide/hql/HQLTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,28 @@ public void test_searched_case_expressions_example_2() {
17381738
});
17391739
}
17401740

1741+
@Test
1742+
public void test_case_arithmetic_expressions_example() {
1743+
doInJPA( this::entityManagerFactory, entityManager -> {
1744+
//tag::hql-case-arithmetic-expressions-example[]
1745+
List<Long> values = entityManager.createQuery(
1746+
"select " +
1747+
" case when p.nickName is null " +
1748+
" then (p.id * 1000) " +
1749+
" else p.id " +
1750+
" end " +
1751+
"from Person p " +
1752+
"order by p.id", Long.class)
1753+
.getResultList();
1754+
1755+
assertEquals(3, values.size());
1756+
assertEquals( 1L, (long) values.get( 0 ) );
1757+
assertEquals( 2000, (long) values.get( 1 ) );
1758+
assertEquals( 3000, (long) values.get( 2 ) );
1759+
//end::hql-case-arithmetic-expressions-example[]
1760+
});
1761+
}
1762+
17411763
@Test
17421764
public void test_hql_null_if_example_1() {
17431765
doInJPA( this::entityManagerFactory, entityManager -> {

hibernate-core/src/test/java/org/hibernate/test/hql/CaseStatementTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.hibernate.test.hql;
88

9+
import java.util.List;
910
import javax.persistence.Entity;
1011
import javax.persistence.Id;
1112

@@ -14,11 +15,14 @@
1415
import org.hibernate.Transaction;
1516
import org.hibernate.engine.spi.SessionImplementor;
1617

18+
import org.hibernate.testing.TestForIssue;
1719
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
1820
import org.junit.Test;
1921

2022
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
23+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
2124
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
25+
import static org.junit.Assert.assertEquals;
2226
import static org.junit.Assert.fail;
2327

2428
/**
@@ -128,6 +132,29 @@ public void testSearchedCaseStatementFixture() {
128132
s.close();
129133
}
130134

135+
@Test
136+
@TestForIssue( jiraKey = "HHH-13095" )
137+
public void testSearchedCaseStatementArithmeticExpression() {
138+
doInHibernate( this::sessionFactory, session -> {
139+
Person steve = new Person();
140+
steve.id = 1;
141+
steve.name = "Steve";
142+
session.persist( steve );
143+
144+
Person brian = new Person();
145+
brian.id = 2;
146+
brian.name = "Brian";
147+
session.persist( brian );
148+
149+
List<Integer> values = session.createQuery(
150+
"select case when p.name = 'Steve' then (p.id * 10) else p.id end from Person p order by p.id" )
151+
.getResultList();
152+
153+
assertEquals( 10, (int) values.get( 0 ) );
154+
assertEquals( 2, (int) values.get( 1 ) );
155+
} );
156+
}
157+
131158
@Test
132159
public void testSearchedCaseStatementWithParamResult() {
133160
Session s = openSession();

0 commit comments

Comments
 (0)