2323import java .util .function .Function ;
2424import java .util .stream .Stream ;
2525
26+ import javax .persistence .EntityManager ;
27+
2628import org .springframework .dao .IncorrectResultSizeDataAccessException ;
2729import org .springframework .data .domain .Page ;
2830import org .springframework .data .domain .PageImpl ;
2931import org .springframework .data .domain .Pageable ;
3032import 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 ;
3433import org .springframework .data .repository .query .FluentQuery .FetchableFluentQuery ;
3534import org .springframework .data .support .PageableExecutionUtils ;
3635import org .springframework .util .Assert ;
@@ -56,34 +55,32 @@ class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> imp
5655 private final BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder ;
5756 private final Function <Predicate , Long > countOperation ;
5857 private final Function <Predicate , Boolean > existsOperation ;
59- private final Projector < AbstractJPAQuery <?, ?>> projector ;
58+ private final EntityManager entityManager ;
6059
6160 public FetchableFluentQueryByPredicate (Predicate predicate , Class <S > entityType ,
6261 Function <Sort , AbstractJPAQuery <?, ?>> finder , BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder ,
6362 Function <Predicate , Long > countOperation , Function <Predicate , Boolean > existsOperation ,
64- MappingContext <? extends PersistentEntity <?, ?>, ? extends PersistentProperty <?>> context ,
65- Projector <AbstractJPAQuery <?, ?>> projector ) {
63+ EntityManager entityManager ) {
6664 this (predicate , entityType , (Class <R >) entityType , Sort .unsorted (), Collections .emptySet (), finder , pagedFinder ,
67- countOperation , existsOperation , context , projector );
65+ countOperation , existsOperation , entityManager );
6866 }
6967
7068 private FetchableFluentQueryByPredicate (Predicate predicate , Class <S > entityType , Class <R > resultType , Sort sort ,
7169 Collection <String > properties , Function <Sort , AbstractJPAQuery <?, ?>> finder ,
7270 BiFunction <Sort , Pageable , AbstractJPAQuery <?, ?>> pagedFinder , Function <Predicate , Long > countOperation ,
7371 Function <Predicate , Boolean > existsOperation ,
74- MappingContext <? extends PersistentEntity <?, ?>, ? extends PersistentProperty <?>> context ,
75- Projector <AbstractJPAQuery <?, ?>> projector ) {
72+ EntityManager entityManager ) {
7673
77- super (resultType , sort , properties , context , entityType );
74+ super (resultType , sort , properties , entityType );
7875 this .predicate = predicate ;
7976 this .finder = finder ;
8077 this .pagedFinder = pagedFinder ;
8178 this .countOperation = countOperation ;
8279 this .existsOperation = existsOperation ;
83- this .projector = projector ;
80+ this .entityManager = entityManager ;
8481 }
8582
86- /*
83+ /*
8784 * (non-Javadoc)
8885 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#sortBy(org.springframework.data.domain.Sort)
8986 */
@@ -93,10 +90,10 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
9390 Assert .notNull (sort , "Sort must not be null!" );
9491
9592 return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort .and (sort ), properties , finder ,
96- pagedFinder , countOperation , existsOperation , context , projector );
93+ pagedFinder , countOperation , existsOperation , entityManager );
9794 }
9895
99- /*
96+ /*
10097 * (non-Javadoc)
10198 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#as(java.lang.Class)
10299 */
@@ -110,21 +107,21 @@ public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) {
110107 }
111108
112109 return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort , properties , finder ,
113- pagedFinder , countOperation , existsOperation , context , projector );
110+ pagedFinder , countOperation , existsOperation , entityManager );
114111 }
115112
116- /*
113+ /*
117114 * (non-Javadoc)
118115 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#project(java.util.Collection)
119116 */
120117 @ Override
121118 public FetchableFluentQuery <R > project (Collection <String > properties ) {
122119
123120 return new FetchableFluentQueryByPredicate <>(predicate , entityType , resultType , sort , mergeProperties (properties ),
124- finder , pagedFinder , countOperation , existsOperation , context , projector );
121+ finder , pagedFinder , countOperation , existsOperation , entityManager );
125122 }
126123
127- /*
124+ /*
128125 * (non-Javadoc)
129126 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#oneValue()
130127 */
@@ -142,7 +139,7 @@ public R oneValue() {
142139 return results .isEmpty () ? null : getConversionFunction ().apply (results .get (0 ));
143140 }
144141
145- /*
142+ /*
146143 * (non-Javadoc)
147144 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#firstValue()
148145 */
@@ -156,7 +153,7 @@ public R firstValue() {
156153 return results .isEmpty () ? null : getConversionFunction ().apply (results .get (0 ));
157154 }
158155
159- /*
156+ /*
160157 * (non-Javadoc)
161158 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
162159 */
@@ -165,7 +162,7 @@ public List<R> all() {
165162 return convert (createSortedAndProjectedQuery ().fetch ());
166163 }
167164
168- /*
165+ /*
169166 * (non-Javadoc)
170167 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#page(org.springframework.data.domain.Pageable)
171168 */
@@ -174,7 +171,7 @@ public Page<R> page(Pageable pageable) {
174171 return pageable .isUnpaged () ? new PageImpl <>(all ()) : readPage (pageable );
175172 }
176173
177- /*
174+ /*
178175 * (non-Javadoc)
179176 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#stream()
180177 */
@@ -186,7 +183,7 @@ public Stream<R> stream() {
186183 .map (getConversionFunction ());
187184 }
188185
189- /*
186+ /*
190187 * (non-Javadoc)
191188 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#count()
192189 */
@@ -195,7 +192,7 @@ public long count() {
195192 return countOperation .apply (predicate );
196193 }
197194
198- /*
195+ /*
199196 * (non-Javadoc)
200197 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#exists()
201198 */
@@ -206,8 +203,12 @@ public boolean exists() {
206203
207204 private AbstractJPAQuery <?, ?> createSortedAndProjectedQuery () {
208205
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+
211212 return query ;
212213 }
213214
0 commit comments