Skip to content

Commit e9f0931

Browse files
committed
HHH-15482 Add test for issue
1 parent 165bb9c commit e9f0931

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.query.criteria;
8+
9+
import java.util.List;
10+
11+
import org.hibernate.testing.TestForIssue;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.ManyToOne;
20+
import jakarta.persistence.Table;
21+
import jakarta.persistence.Tuple;
22+
import jakarta.persistence.criteria.CriteriaBuilder;
23+
import jakarta.persistence.criteria.CriteriaQuery;
24+
import jakarta.persistence.criteria.Join;
25+
import jakarta.persistence.criteria.Root;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
@Jpa(
30+
annotatedClasses = {
31+
SelectCaseTest.Address.class,
32+
SelectCaseTest.Person.class
33+
}
34+
)
35+
@TestForIssue(jiraKey = "HHH-15482")
36+
public class SelectCaseTest {
37+
38+
@BeforeEach
39+
public void setUp(EntityManagerFactoryScope scope) {
40+
scope.inTransaction(
41+
entityManager -> {
42+
Address address = new Address( "RM", "Via Fori Imperiali", "Roma" );
43+
Person person = new Person( 1, "Roberto", 70, address );
44+
entityManager.persist( address );
45+
entityManager.persist( person );
46+
47+
Address address2 = new Address( "GR", "Via Roma", "Gradoli" );
48+
Person person2 = new Person( 2, "Andrea", 50, address2 );
49+
entityManager.persist( address2 );
50+
entityManager.persist( person2 );
51+
}
52+
);
53+
}
54+
55+
@Test
56+
public void testSelectCaseStringExpressionReturningAnIntegerValues(EntityManagerFactoryScope scope) {
57+
scope.inTransaction(
58+
entityManager -> {
59+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
60+
CriteriaQuery<Tuple> criteriaQuery = cb.createTupleQuery();
61+
Root<Person> personRoot = criteriaQuery.from( Person.class );
62+
63+
Join<Object, Object> secondaryJoin = personRoot.join( "address" );
64+
criteriaQuery.multiselect(
65+
cb.selectCase( secondaryJoin.get( "code" ) )
66+
.when( "GR", personRoot.get( "age" ) )
67+
.otherwise( cb.nullLiteral( Integer.class ) )
68+
.alias( "person_age" )
69+
).orderBy( cb.asc( personRoot.get( "id" ) ) );
70+
71+
List<Tuple> ages = entityManager.createQuery( criteriaQuery ).getResultList();
72+
assertThat( ages.size() ).isEqualTo( 2 );
73+
assertThat( ages.get( 0 ).get( "person_age" ) ).isNull();
74+
assertThat( ages.get( 1 ).get( "person_age" ) ).isEqualTo( 50 );
75+
}
76+
);
77+
78+
}
79+
80+
@Entity
81+
@Table(name = "PERSON_TABLE")
82+
public class Person {
83+
84+
@Id
85+
private int id;
86+
87+
private String name;
88+
89+
private Integer age;
90+
91+
@ManyToOne
92+
private Address address;
93+
94+
public Person() {
95+
}
96+
97+
public Person(int id, String name, Integer age, Address address) {
98+
this.id = id;
99+
this.name = name;
100+
this.age = age;
101+
this.address = address;
102+
}
103+
}
104+
105+
@Entity
106+
@Table(name = "ADDRESS_TABLE")
107+
public static class Address {
108+
109+
@Id
110+
private String code;
111+
112+
private String street;
113+
114+
private String city;
115+
116+
public Address() {
117+
}
118+
119+
public Address(String code, String street, String city) {
120+
this.code = code;
121+
this.street = street;
122+
this.city = city;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)