20
20
import java .util .List ;
21
21
import java .util .Optional ;
22
22
23
+ import org .springframework .dao .IncorrectResultSizeDataAccessException ;
23
24
import org .springframework .data .domain .Example ;
24
25
import org .springframework .data .domain .Page ;
25
26
import org .springframework .data .domain .PageImpl ;
29
30
import org .springframework .data .redis .core .RedisKeyValueTemplate ;
30
31
import org .springframework .data .redis .core .convert .IndexResolver ;
31
32
import org .springframework .data .redis .core .convert .PathIndexResolver ;
32
- import org .springframework .data .redis .core .convert .RedisConverter ;
33
33
import org .springframework .data .redis .repository .query .ExampleQueryMapper ;
34
34
import org .springframework .data .redis .repository .query .RedisOperationChain ;
35
35
import org .springframework .data .repository .core .EntityInformation ;
43
43
* methods.
44
44
*
45
45
* @author Mark Paluch
46
+ * @author Christoph Strobl
46
47
* @since 2.1
47
48
*/
48
49
@ SuppressWarnings ("unchecked" )
@@ -62,17 +63,9 @@ public class QueryByExampleRedisExecutor<T> implements QueryByExampleExecutor<T>
62
63
public QueryByExampleRedisExecutor (EntityInformation <T , ?> entityInformation ,
63
64
RedisKeyValueTemplate keyValueTemplate ) {
64
65
65
- Assert .notNull (entityInformation , "EntityInformation must not be null!" );
66
- Assert .notNull (keyValueTemplate , "RedisKeyValueTemplate must not be null!" );
67
-
68
- this .entityInformation = entityInformation ;
69
- this .keyValueTemplate = keyValueTemplate ;
70
-
71
- RedisConverter converter = keyValueTemplate .getAdapter ().getConverter ();
72
- IndexResolver indexResolver = converter .getIndexResolver ();
73
-
74
- this .mapper = new ExampleQueryMapper (keyValueTemplate .getMappingContext (),
75
- indexResolver != null ? indexResolver : new PathIndexResolver (converter .getMappingContext ()));
66
+ this (entityInformation , keyValueTemplate ,
67
+ keyValueTemplate .getConverter ().getIndexResolver () != null ? keyValueTemplate .getConverter ().getIndexResolver ()
68
+ : new PathIndexResolver (keyValueTemplate .getMappingContext ()));
76
69
}
77
70
78
71
/**
@@ -91,8 +84,7 @@ public QueryByExampleRedisExecutor(EntityInformation<T, ?> entityInformation, Re
91
84
this .entityInformation = entityInformation ;
92
85
this .keyValueTemplate = keyValueTemplate ;
93
86
94
- this .mapper = new ExampleQueryMapper (keyValueTemplate .getMappingContext (),
95
- new PathIndexResolver (keyValueTemplate .getMappingContext ()));
87
+ this .mapper = new ExampleQueryMapper (keyValueTemplate .getMappingContext (), indexResolver );
96
88
}
97
89
98
90
/*
@@ -102,13 +94,21 @@ public QueryByExampleRedisExecutor(EntityInformation<T, ?> entityInformation, Re
102
94
@ Override
103
95
public <S extends T > Optional <S > findOne (Example <S > example ) {
104
96
105
- RedisOperationChain operationChain = getQuery (example );
97
+ RedisOperationChain operationChain = createQuery (example );
106
98
107
99
KeyValueQuery <RedisOperationChain > query = new KeyValueQuery <>(operationChain );
108
- Iterable <T > result = keyValueTemplate .find (query .limit (1 ), entityInformation .getJavaType ());
109
- Iterator <T > iterator = result .iterator ();
100
+ Iterator <T > iterator = keyValueTemplate .find (query .limit (2 ), entityInformation .getJavaType ()).iterator ();
101
+
102
+ Optional result = Optional .empty ();
103
+
104
+ if (iterator .hasNext ()) {
105
+ result = Optional .of ((S ) iterator .next ());
106
+ if (iterator .hasNext ()) {
107
+ throw new IncorrectResultSizeDataAccessException (1 );
108
+ }
109
+ }
110
110
111
- return iterator . hasNext () ? Optional . of (( S ) iterator . next ()) : Optional . empty () ;
111
+ return result ;
112
112
}
113
113
114
114
/*
@@ -118,7 +118,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
118
118
@ Override
119
119
public <S extends T > Iterable <S > findAll (Example <S > example ) {
120
120
121
- RedisOperationChain operationChain = getQuery (example );
121
+ RedisOperationChain operationChain = createQuery (example );
122
122
123
123
return (Iterable <S >) keyValueTemplate .find (new KeyValueQuery <>(operationChain ), entityInformation .getJavaType ());
124
124
}
@@ -141,7 +141,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
141
141
142
142
Assert .notNull (pageable , "Pageable must not be null!" );
143
143
144
- RedisOperationChain operationChain = getQuery (example );
144
+ RedisOperationChain operationChain = createQuery (example );
145
145
146
146
KeyValueQuery <RedisOperationChain > query = new KeyValueQuery <>(operationChain );
147
147
Iterable <T > result = keyValueTemplate .find (
@@ -166,7 +166,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
166
166
@ Override
167
167
public <S extends T > long count (Example <S > example ) {
168
168
169
- RedisOperationChain operationChain = getQuery (example );
169
+ RedisOperationChain operationChain = createQuery (example );
170
170
171
171
return keyValueTemplate .count (new KeyValueQuery <>(operationChain ), entityInformation .getJavaType ());
172
172
}
@@ -180,7 +180,7 @@ public <S extends T> boolean exists(Example<S> example) {
180
180
return count (example ) > 0 ;
181
181
}
182
182
183
- private <S extends T > RedisOperationChain getQuery (Example <S > example ) {
183
+ private <S extends T > RedisOperationChain createQuery (Example <S > example ) {
184
184
185
185
Assert .notNull (example , "Example must not be null!" );
186
186
0 commit comments