1
1
/*
2
- * Copyright 2019-2021 the original author or authors.
2
+ * Copyright 2019-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .math .BigDecimal ;
24
24
import java .sql .JDBCType ;
25
25
import java .util .Date ;
26
+ import java .util .List ;
27
+ import java .util .Set ;
26
28
27
29
import org .junit .jupiter .api .Test ;
28
30
import org .junit .jupiter .api .extension .ExtendWith ;
36
38
import org .springframework .data .convert .WritingConverter ;
37
39
import org .springframework .data .jdbc .core .convert .JdbcCustomConversions ;
38
40
import org .springframework .data .jdbc .core .mapping .JdbcValue ;
41
+ import org .springframework .data .jdbc .repository .query .Query ;
39
42
import org .springframework .data .jdbc .repository .support .JdbcRepositoryFactory ;
40
43
import org .springframework .data .jdbc .testing .AssumeFeatureTestExecutionListener ;
41
44
import org .springframework .data .jdbc .testing .TestConfiguration ;
50
53
*
51
54
* @author Jens Schauder
52
55
* @author Sanghyuk Jung
56
+ * @author Chirag Tailor
53
57
*/
54
58
@ ContextConfiguration
55
59
@ Transactional
@@ -69,18 +73,19 @@ Class<?> testClass() {
69
73
}
70
74
71
75
@ Bean
72
- EntityWithBooleanRepository repository () {
73
- return factory .getRepository (EntityWithBooleanRepository .class );
76
+ EntityWithStringyBigDecimalRepository repository () {
77
+ return factory .getRepository (EntityWithStringyBigDecimalRepository .class );
74
78
}
75
79
76
80
@ Bean
77
81
JdbcCustomConversions jdbcCustomConversions () {
78
82
return new JdbcCustomConversions (asList (StringToBigDecimalConverter .INSTANCE , BigDecimalToString .INSTANCE ,
79
- CustomIdReadingConverter .INSTANCE , CustomIdWritingConverter .INSTANCE ));
83
+ CustomIdReadingConverter .INSTANCE , CustomIdWritingConverter .INSTANCE , DirectionToIntegerConverter .INSTANCE ,
84
+ NumberToDirectionConverter .INSTANCE , IntegerToDirectionConverter .INSTANCE ));
80
85
}
81
86
}
82
87
83
- @ Autowired EntityWithBooleanRepository repository ;
88
+ @ Autowired EntityWithStringyBigDecimalRepository repository ;
84
89
85
90
/**
86
91
* In PostrgreSQL this fails if a simple converter like the following is used.
@@ -143,13 +148,52 @@ public void saveAndLoadAnEntityWithReference() {
143
148
});
144
149
}
145
150
146
- interface EntityWithBooleanRepository extends CrudRepository <EntityWithStringyBigDecimal , CustomId > {}
151
+ @ Test // GH-1212
152
+ void queryByEnumTypeIn () {
153
+
154
+ EntityWithStringyBigDecimal entityA = new EntityWithStringyBigDecimal ();
155
+ entityA .direction = Direction .LEFT ;
156
+ EntityWithStringyBigDecimal entityB = new EntityWithStringyBigDecimal ();
157
+ entityB .direction = Direction .CENTER ;
158
+ EntityWithStringyBigDecimal entityC = new EntityWithStringyBigDecimal ();
159
+ entityC .direction = Direction .RIGHT ;
160
+ repository .saveAll (asList (entityA , entityB , entityC ));
161
+
162
+ assertThat (repository .findByEnumTypeIn (Set .of (Direction .LEFT , Direction .RIGHT )))
163
+ .extracting (entity -> entity .direction )
164
+ .containsExactlyInAnyOrder (Direction .LEFT , Direction .RIGHT );
165
+ }
166
+
167
+ @ Test // GH-1212
168
+ void queryByEnumTypeEqual () {
169
+
170
+ EntityWithStringyBigDecimal entityA = new EntityWithStringyBigDecimal ();
171
+ entityA .direction = Direction .LEFT ;
172
+ EntityWithStringyBigDecimal entityB = new EntityWithStringyBigDecimal ();
173
+ entityB .direction = Direction .CENTER ;
174
+ EntityWithStringyBigDecimal entityC = new EntityWithStringyBigDecimal ();
175
+ entityC .direction = Direction .RIGHT ;
176
+ repository .saveAll (asList (entityA , entityB , entityC ));
177
+
178
+ assertThat (repository .findByEnumTypeIn (Set .of (Direction .CENTER )))
179
+ .extracting (entity -> entity .direction )
180
+ .containsExactly (Direction .CENTER );
181
+ }
182
+
183
+ interface EntityWithStringyBigDecimalRepository extends CrudRepository <EntityWithStringyBigDecimal , CustomId > {
184
+ @ Query ("SELECT * FROM ENTITY_WITH_STRINGY_BIG_DECIMAL WHERE DIRECTION IN (:types)" )
185
+ List <EntityWithStringyBigDecimal > findByEnumTypeIn (Set <Direction > types );
186
+
187
+ @ Query ("SELECT * FROM ENTITY_WITH_STRINGY_BIG_DECIMAL WHERE DIRECTION = :type" )
188
+ List <EntityWithStringyBigDecimal > findByEnumType (Direction type );
189
+ }
147
190
148
191
private static class EntityWithStringyBigDecimal {
149
192
150
193
@ Id CustomId id ;
151
- String stringyNumber ;
194
+ String stringyNumber = "1.0" ;
152
195
OtherEntity reference ;
196
+ Direction direction = Direction .CENTER ;
153
197
}
154
198
155
199
private static class CustomId {
@@ -167,6 +211,10 @@ private static class OtherEntity {
167
211
Date created ;
168
212
}
169
213
214
+ enum Direction {
215
+ LEFT , CENTER , RIGHT
216
+ }
217
+
170
218
@ WritingConverter
171
219
enum StringToBigDecimalConverter implements Converter <String , JdbcValue > {
172
220
@@ -214,4 +262,55 @@ public CustomId convert(Number source) {
214
262
}
215
263
}
216
264
265
+ @ WritingConverter
266
+ enum DirectionToIntegerConverter implements Converter <Direction , JdbcValue > {
267
+
268
+ INSTANCE ;
269
+
270
+ @ Override
271
+ public JdbcValue convert (Direction source ) {
272
+
273
+ int integer = switch (source ) {
274
+ case LEFT -> -1 ;
275
+ case CENTER -> 0 ;
276
+ case RIGHT -> 1 ;
277
+ };
278
+ return JdbcValue .of (integer , JDBCType .INTEGER );
279
+ }
280
+ }
281
+
282
+ @ ReadingConverter // Needed for Oracle since the JDBC driver returns BigDecimal on read
283
+ enum NumberToDirectionConverter implements Converter <Number , Direction > {
284
+
285
+ INSTANCE ;
286
+
287
+ @ Override
288
+ public Direction convert (Number source ) {
289
+ int sourceAsInt = source .intValue ();
290
+ if (sourceAsInt == 0 ) {
291
+ return Direction .CENTER ;
292
+ } else if (sourceAsInt < 0 ) {
293
+ return Direction .LEFT ;
294
+ } else {
295
+ return Direction .RIGHT ;
296
+ }
297
+ }
298
+ }
299
+
300
+ @ ReadingConverter
301
+ enum IntegerToDirectionConverter implements Converter <Integer , Direction > {
302
+
303
+ INSTANCE ;
304
+
305
+ @ Override
306
+ public Direction convert (Integer source ) {
307
+ if (source == 0 ) {
308
+ return Direction .CENTER ;
309
+ } else if (source < 0 ) {
310
+ return Direction .LEFT ;
311
+ } else {
312
+ return Direction .RIGHT ;
313
+ }
314
+ }
315
+ }
217
316
}
0 commit comments