Skip to content

Commit aece493

Browse files
dreab8sebersole
authored andcommitted
HHH-15725 Add test for issue
1 parent 03e48d8 commit aece493

File tree

4 files changed

+241
-78
lines changed

4 files changed

+241
-78
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/AbstractCriteriaTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
Thing.class,
5151
ThingWithQuantity.class,
5252
VersionedEntity.class
53-
})
53+
}
54+
)
5455
public abstract class AbstractCriteriaTest {
5556
}

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/basic/CastTest.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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.jpa.criteria.basic;
8+
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
11+
import java.util.List;
12+
13+
import org.hibernate.dialect.DerbyDialect;
14+
import org.hibernate.dialect.SybaseASEDialect;
15+
import org.hibernate.dialect.SybaseDialect;
16+
import org.hibernate.orm.test.jpa.metamodel.Product;
17+
import org.hibernate.orm.test.jpa.metamodel.Product_;
18+
import org.hibernate.orm.test.jpa.metamodel.ShelfLife;
19+
20+
import org.hibernate.testing.jdbc.SQLStatementInspector;
21+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
22+
import org.hibernate.testing.orm.junit.JiraKey;
23+
import org.hibernate.testing.orm.junit.Jpa;
24+
import org.hibernate.testing.orm.junit.SkipForDialect;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
30+
import jakarta.persistence.AttributeConverter;
31+
import jakarta.persistence.Convert;
32+
import jakarta.persistence.Entity;
33+
import jakarta.persistence.Id;
34+
import jakarta.persistence.criteria.CriteriaBuilder;
35+
import jakarta.persistence.criteria.CriteriaQuery;
36+
import jakarta.persistence.criteria.Root;
37+
38+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
39+
import static org.junit.jupiter.api.Assertions.assertFalse;
40+
41+
@Jpa(annotatedClasses = {
42+
Product.class,
43+
ShelfLife.class,
44+
CiteriaAsExpressionTest.TestEntity.class
45+
},
46+
useCollectingStatementInspector = true
47+
)
48+
public class CiteriaAsExpressionTest {
49+
private static final int QUANTITY = 2;
50+
private static final String NAME = "a";
51+
private static final Integer NAME_CONVERTED_VALUE = 1;
52+
53+
@BeforeEach
54+
public void setup(EntityManagerFactoryScope scope) {
55+
scope.inTransaction(
56+
entityManager -> {
57+
Product product = new Product();
58+
product.setId( "product1" );
59+
product.setPrice( 1.23d );
60+
product.setQuantity( QUANTITY );
61+
product.setPartNumber( ( (long) Integer.MAX_VALUE ) + 1 );
62+
product.setRating( 1.999f );
63+
product.setSomeBigInteger( BigInteger.valueOf( 987654321 ) );
64+
product.setSomeBigDecimal( BigDecimal.valueOf( 987654.321 ) );
65+
entityManager.persist( product );
66+
67+
TestEntity testEntity = new TestEntity( 1, NAME );
68+
entityManager.persist( testEntity );
69+
}
70+
);
71+
}
72+
73+
@AfterEach
74+
public void tearDown(EntityManagerFactoryScope scope) {
75+
scope.inTransaction(
76+
entityManager -> {
77+
entityManager.createQuery( "delete from Product" ).executeUpdate();
78+
entityManager.createQuery( "delete from TestEntity" ).executeUpdate();
79+
}
80+
);
81+
}
82+
83+
@Test
84+
@JiraKey("HHH-5755")
85+
@SkipForDialect(dialectClass = DerbyDialect.class )
86+
@SkipForDialect(dialectClass = SybaseDialect.class )
87+
@SkipForDialect(dialectClass = SybaseASEDialect.class )
88+
public void testAsToString(EntityManagerFactoryScope scope) {
89+
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
90+
statementInspector.clear();
91+
scope.inTransaction(
92+
entityManager -> {
93+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
94+
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
95+
Root<Product> root = criteria.from( Product.class );
96+
criteria.where( builder.equal(
97+
root.get( Product_.quantity ).as( String.class ),
98+
builder.literal( String.valueOf( QUANTITY ) )
99+
) );
100+
List<Product> result = entityManager.createQuery( criteria ).getResultList();
101+
Assertions.assertEquals( 1, result.size() );
102+
103+
assertExecuteQueryDoesNotContainACast( statementInspector );
104+
}
105+
);
106+
}
107+
108+
@Test
109+
@JiraKey("HHH-15713")
110+
public void testAsIntegerToIntegreDoesNotCreateACast(EntityManagerFactoryScope scope) {
111+
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
112+
statementInspector.clear();
113+
scope.inTransaction(
114+
entityManager -> {
115+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
116+
CriteriaQuery<Product> criteria = builder.createQuery( Product.class );
117+
Root<Product> root = criteria.from( Product.class );
118+
criteria.where( builder.equal(
119+
root.get( Product_.quantity ).as( Integer.class ),
120+
builder.literal( QUANTITY )
121+
)
122+
);
123+
List<Product> result = entityManager.createQuery( criteria ).getResultList();
124+
Assertions.assertEquals( 1, result.size() );
125+
126+
assertExecuteQueryDoesNotContainACast( statementInspector );
127+
}
128+
);
129+
}
130+
131+
@Test
132+
@JiraKey("HHH-15725")
133+
public void testAsAndConvertedAttribute(EntityManagerFactoryScope scope) {
134+
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
135+
statementInspector.clear();
136+
scope.inTransaction(
137+
entityManager -> {
138+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
139+
CriteriaQuery<TestEntity> criteria = builder.createQuery( TestEntity.class );
140+
Root<TestEntity> root = criteria.from( TestEntity.class );
141+
criteria.where( builder.equal(
142+
root.get( "name" ).as( Integer.class ),
143+
builder.literal( NAME_CONVERTED_VALUE )
144+
)
145+
);
146+
List<TestEntity> result = entityManager.createQuery( criteria ).getResultList();
147+
Assertions.assertEquals( 1, result.size() );
148+
149+
assertExecuteQueryDoesNotContainACast( statementInspector );
150+
}
151+
);
152+
}
153+
154+
@Test
155+
@JiraKey("HHH-15725")
156+
public void testAsAndConvertedAttribute2(EntityManagerFactoryScope scope) {
157+
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
158+
statementInspector.clear();
159+
160+
scope.inTransaction(
161+
entityManager -> {
162+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
163+
CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class );
164+
Root<TestEntity> root = criteria.from( TestEntity.class );
165+
criteria.select( root.get( "name" ).as( Integer.class ) );
166+
List<Integer> result = entityManager.createQuery( criteria ).getResultList();
167+
Assertions.assertEquals( 1, result.size() );
168+
Integer actual = result.get( 0 );
169+
// When Expression.as() is used the Converted is not applied
170+
171+
Assertions.assertEquals( 1, actual );
172+
173+
assertExecuteQueryDoesNotContainACast( statementInspector );
174+
}
175+
);
176+
}
177+
178+
179+
private static void assertExecuteQueryDoesNotContainACast(SQLStatementInspector statementInspector) {
180+
assertFalse( getExecutedQuery( statementInspector ).contains( "cast" ) );
181+
}
182+
183+
private static String getExecutedQuery(SQLStatementInspector statementInspector) {
184+
List<String> sqlQueries = statementInspector.getSqlQueries();
185+
assertThat( sqlQueries.size() ).isEqualTo( 1 );
186+
187+
String executedQuery = sqlQueries.get( 0 );
188+
return executedQuery;
189+
}
190+
191+
@Entity(name = "TestEntity")
192+
public static class TestEntity {
193+
@Id
194+
private Integer id;
195+
196+
@Convert(converter = TestConverter.class)
197+
private String name;
198+
199+
public TestEntity() {
200+
}
201+
202+
public TestEntity(Integer id, String name) {
203+
this.id = id;
204+
this.name = name;
205+
}
206+
207+
public Integer getId() {
208+
return id;
209+
}
210+
211+
public String getName() {
212+
return name;
213+
}
214+
}
215+
216+
private static class TestConverter implements AttributeConverter<String, Integer> {
217+
@Override
218+
public Integer convertToDatabaseColumn(String attribute) {
219+
if ( attribute.equals( NAME ) ) {
220+
return NAME_CONVERTED_VALUE;
221+
}
222+
return 0;
223+
}
224+
225+
@Override
226+
public String convertToEntityAttribute(Integer dbData) {
227+
if ( dbData == 1 ) {
228+
return "a";
229+
}
230+
return "b";
231+
}
232+
}
233+
234+
}

hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaBuilderNonStandardFunctionsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,14 @@ public void testOverlay(SessionFactoryScope scope) {
237237
Expression<String> theString = from.get( "theString" );
238238
query.multiselect(
239239
cb.overlay( theString, "33", 6 ),
240-
cb.overlay( theString, from.get( "theInt" ).as( String.class ), 6 ),
240+
// cb.overlay( theString, from.get( "theInt" ).as( String.class ), 6 ),
241241
cb.overlay( theString, "1234", from.get( "theInteger" ), 2 )
242242
).where( cb.equal( from.get( "id" ), 4 ) );
243243

244244
Tuple result = session.createQuery( query ).getSingleResult();
245245
assertEquals( "thirt33n", result.get( 0 ) );
246-
assertEquals( "thirt13n", result.get( 1 ) );
247-
assertEquals( "thi1234een", result.get( 2 ) );
246+
// assertEquals( "thirt13n", result.get( 1 ) );
247+
assertEquals( "thi1234een", result.get( 1 ) );
248248
} );
249249
}
250250

@@ -300,12 +300,12 @@ public void testReplace(SessionFactoryScope scope) {
300300
Expression<String> theString = from.get( "theString" );
301301
query.multiselect(
302302
cb.replace( theString, "thi", "12345" ),
303-
cb.replace( theString, "t", from.get( "theInteger" ).as( String.class ) )
303+
cb.replace( theString, "t", from.get( "theString" ) )
304304
).where( cb.equal( from.get( "id" ), 4 ) );
305305

306306
Tuple result = session.createQuery( query ).getSingleResult();
307307
assertEquals( "12345rteen", result.get( 0 ) );
308-
assertEquals( "4hir4een", result.get( 1 ) );
308+
assertEquals( "thirteenhirthirteeneen", result.get( 1 ) );
309309
} );
310310
}
311311

0 commit comments

Comments
 (0)