23
23
import java .util .function .Function ;
24
24
import java .util .stream .Stream ;
25
25
26
+ import javax .persistence .EntityManager ;
27
+
26
28
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
27
29
import org .springframework .data .domain .Page ;
28
30
import org .springframework .data .domain .PageImpl ;
29
31
import org .springframework .data .domain .Pageable ;
30
32
import org .springframework .data .domain .Sort ;
31
- import org .springframework .data .mapping .PersistentEntity ;
32
- import org .springframework .data .mapping .PersistentProperty ;
33
- import org .springframework .data .mapping .context .MappingContext ;
34
33
import org .springframework .data .repository .query .FluentQuery .FetchableFluentQuery ;
35
34
import org .springframework .data .support .PageableExecutionUtils ;
36
35
import org .springframework .util .Assert ;
@@ -56,34 +55,32 @@ class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> imp
56
55
private final BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder ;
57
56
private final Function <Predicate , Long > countOperation ;
58
57
private final Function <Predicate , Boolean > existsOperation ;
59
- private final Projector < AbstractJPAQuery <?, ?>> projector ;
58
+ private final EntityManager entityManager ;
60
59
61
60
public FetchableFluentQueryByPredicate (Predicate predicate , Class <S > entityType ,
62
61
Function <Sort , AbstractJPAQuery <?, ?>> finder , BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder ,
63
62
Function <Predicate , Long > countOperation , Function <Predicate , Boolean > existsOperation ,
64
- MappingContext <? extends PersistentEntity <?, ?>, ? extends PersistentProperty <?>> context ,
65
- Projector <AbstractJPAQuery <?, ?>> projector ) {
63
+ EntityManager entityManager ) {
66
64
this (predicate , entityType , (Class <R >) entityType , Sort .unsorted (), Collections .emptySet (), finder , pagedFinder ,
67
- countOperation , existsOperation , context , projector );
65
+ countOperation , existsOperation , entityManager );
68
66
}
69
67
70
68
private FetchableFluentQueryByPredicate (Predicate predicate , Class <S > entityType , Class <R > resultType , Sort sort ,
71
69
Collection <String > properties , Function <Sort , AbstractJPAQuery <?, ?>> finder ,
72
70
BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder , Function <Predicate , Long > countOperation ,
73
71
Function <Predicate , Boolean > existsOperation ,
74
- MappingContext <? extends PersistentEntity <?, ?>, ? extends PersistentProperty <?>> context ,
75
- Projector <AbstractJPAQuery <?, ?>> projector ) {
72
+ EntityManager entityManager ) {
76
73
77
- super (resultType , sort , properties , context , entityType );
74
+ super (resultType , sort , properties , entityType );
78
75
this .predicate = predicate ;
79
76
this .finder = finder ;
80
77
this .pagedFinder = pagedFinder ;
81
78
this .countOperation = countOperation ;
82
79
this .existsOperation = existsOperation ;
83
- this .projector = projector ;
80
+ this .entityManager = entityManager ;
84
81
}
85
82
86
- /*
83
+ /*
87
84
* (non-Javadoc)
88
85
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#sortBy(org.springframework.data.domain.Sort)
89
86
*/
@@ -93,10 +90,10 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
93
90
Assert .notNull (sort , "Sort must not be null!" );
94
91
95
92
return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort .and (sort ), properties , finder ,
96
- pagedFinder , countOperation , existsOperation , context , projector );
93
+ pagedFinder , countOperation , existsOperation , entityManager );
97
94
}
98
95
99
- /*
96
+ /*
100
97
* (non-Javadoc)
101
98
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#as(java.lang.Class)
102
99
*/
@@ -110,21 +107,21 @@ public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) {
110
107
}
111
108
112
109
return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort , properties , finder ,
113
- pagedFinder , countOperation , existsOperation , context , projector );
110
+ pagedFinder , countOperation , existsOperation , entityManager );
114
111
}
115
112
116
- /*
113
+ /*
117
114
* (non-Javadoc)
118
115
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#project(java.util.Collection)
119
116
*/
120
117
@ Override
121
118
public FetchableFluentQuery <R > project (Collection <String > properties ) {
122
119
123
120
return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort , mergeProperties (properties ),
124
- finder , pagedFinder , countOperation , existsOperation , context , projector );
121
+ finder , pagedFinder , countOperation , existsOperation , entityManager );
125
122
}
126
123
127
- /*
124
+ /*
128
125
* (non-Javadoc)
129
126
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#oneValue()
130
127
*/
@@ -142,7 +139,7 @@ public R oneValue() {
142
139
return results .isEmpty () ? null : getConversionFunction ().apply (results .get (0 ));
143
140
}
144
141
145
- /*
142
+ /*
146
143
* (non-Javadoc)
147
144
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#firstValue()
148
145
*/
@@ -156,7 +153,7 @@ public R firstValue() {
156
153
return results .isEmpty () ? null : getConversionFunction ().apply (results .get (0 ));
157
154
}
158
155
159
- /*
156
+ /*
160
157
* (non-Javadoc)
161
158
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
162
159
*/
@@ -165,7 +162,7 @@ public List<R> all() {
165
162
return convert (createSortedAndProjectedQuery ().fetch ());
166
163
}
167
164
168
- /*
165
+ /*
169
166
* (non-Javadoc)
170
167
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#page(org.springframework.data.domain.Pageable)
171
168
*/
@@ -174,7 +171,7 @@ public Page<R> page(Pageable pageable) {
174
171
return pageable .isUnpaged () ? new PageImpl <>(all ()) : readPage (pageable );
175
172
}
176
173
177
- /*
174
+ /*
178
175
* (non-Javadoc)
179
176
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#stream()
180
177
*/
@@ -186,7 +183,7 @@ public Stream<R> stream() {
186
183
.map (getConversionFunction ());
187
184
}
188
185
189
- /*
186
+ /*
190
187
* (non-Javadoc)
191
188
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#count()
192
189
*/
@@ -195,7 +192,7 @@ public long count() {
195
192
return countOperation .apply (predicate );
196
193
}
197
194
198
- /*
195
+ /*
199
196
* (non-Javadoc)
200
197
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#exists()
201
198
*/
@@ -206,8 +203,12 @@ public boolean exists() {
206
203
207
204
private AbstractJPAQuery <?, ?> createSortedAndProjectedQuery () {
208
205
209
- final AbstractJPAQuery <?, ?> query = finder .apply (sort );
210
- projector .apply (entityType , query , properties );
206
+ AbstractJPAQuery <?, ?> query = finder .apply (sort );
207
+
208
+ if (!properties .isEmpty ()) {
209
+ query .setHint (EntityGraphFactory .HINT , EntityGraphFactory .create (entityManager , entityType , properties ));
210
+ }
211
+
211
212
return query ;
212
213
}
213
214
0 commit comments