38
38
import java .util .List ;
39
39
import java .util .Map ;
40
40
import java .util .Optional ;
41
+ import java .util .function .BiConsumer ;
41
42
import java .util .function .Function ;
42
43
import java .util .stream .Collectors ;
43
44
import java .util .stream .StreamSupport ;
@@ -225,9 +226,11 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
225
226
entityInformation .getIdAttribute ().getName ());
226
227
227
228
Query query = em .createQuery (queryString );
228
- /**
229
+
230
+ /*
229
231
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
230
232
*/
233
+
231
234
if (Collection .class .isInstance (ids )) {
232
235
query .setParameter ("ids" , ids );
233
236
} else {
@@ -299,33 +302,11 @@ public Optional<T> findById(ID id) {
299
302
}
300
303
301
304
LockModeType type = metadata .getLockModeType ();
302
-
303
- Map <String , Object > hints = new HashMap <>();
304
-
305
- getQueryHints ().withFetchGraphs (em ).forEach (hints ::put );
306
-
307
- if (metadata != null && metadata .getComment () != null && provider .getCommentHintKey () != null ) {
308
- hints .put (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
309
- }
305
+ Map <String , Object > hints = getHints ();
310
306
311
307
return Optional .ofNullable (type == null ? em .find (domainType , id , hints ) : em .find (domainType , id , type , hints ));
312
308
}
313
309
314
- /**
315
- * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
316
- * {@link EntityGraph} information.
317
- */
318
- protected QueryHints getQueryHints () {
319
- return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
320
- }
321
-
322
- /**
323
- * Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
324
- */
325
- protected QueryHints getQueryHintsForCount () {
326
- return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata ).forCounts ();
327
- }
328
-
329
310
@ Deprecated
330
311
@ Override
331
312
public T getOne (ID id ) {
@@ -432,7 +413,7 @@ public List<T> findAll(Sort sort) {
432
413
@ Override
433
414
public Page <T > findAll (Pageable pageable ) {
434
415
435
- if (isUnpaged (pageable )) {
416
+ if (pageable . isUnpaged ()) {
436
417
return new PageImpl <>(findAll ());
437
418
}
438
419
@@ -458,7 +439,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
458
439
public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
459
440
460
441
TypedQuery <T > query = getQuery (spec , pageable );
461
- return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
442
+ return pageable . isUnpaged () ? new PageImpl <>(query .getResultList ())
462
443
: readPage (query , getDomainClass (), pageable , spec );
463
444
}
464
445
@@ -489,9 +470,12 @@ public <S extends T> long count(Example<S> example) {
489
470
public <S extends T > boolean exists (Example <S > example ) {
490
471
491
472
Specification <S > spec = new ExampleSpecification <>(example , this .escapeCharacter );
492
- CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder ().createQuery (Integer .class );
493
- cq .select (this .em .getCriteriaBuilder ().literal (1 ));
473
+ CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder () //
474
+ .createQuery (Integer .class ) //
475
+ .select (this .em .getCriteriaBuilder ().literal (1 ));
476
+
494
477
applySpecificationToCriteria (spec , example .getProbeType (), cq );
478
+
495
479
TypedQuery <Integer > query = applyRepositoryMethodMetadata (this .em .createQuery (cq ));
496
480
return query .setMaxResults (1 ).getResultList ().size () == 1 ;
497
481
}
@@ -541,7 +525,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
541
525
Class <S > probeType = example .getProbeType ();
542
526
TypedQuery <S > query = getQuery (new ExampleSpecification <>(example , escapeCharacter ), probeType , pageable );
543
527
544
- return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ()) : readPage (query , probeType , pageable , spec );
528
+ return pageable . isUnpaged () ? new PageImpl <>(query .getResultList ()) : readPage (query , probeType , pageable , spec );
545
529
}
546
530
547
531
@ Override
@@ -777,6 +761,21 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
777
761
return applyRepositoryMethodMetadataForCount (em .createQuery (query ));
778
762
}
779
763
764
+ /**
765
+ * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
766
+ * {@link EntityGraph} information.
767
+ */
768
+ protected QueryHints getQueryHints () {
769
+ return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
770
+ }
771
+
772
+ /**
773
+ * Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
774
+ */
775
+ protected QueryHints getQueryHintsForCount () {
776
+ return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata ).forCounts ();
777
+ }
778
+
780
779
/**
781
780
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
782
781
*
@@ -827,10 +826,7 @@ private void applyQueryHints(Query query) {
827
826
}
828
827
829
828
getQueryHints ().withFetchGraphs (em ).forEach (query ::setHint );
830
-
831
- if (metadata .getComment () != null && provider .getCommentHintKey () != null ) {
832
- query .setHint (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
833
- }
829
+ applyComment (metadata , query ::setHint );
834
830
}
835
831
836
832
private <S > TypedQuery <S > applyRepositoryMethodMetadataForCount (TypedQuery <S > query ) {
@@ -851,9 +847,26 @@ private void applyQueryHintsForCount(Query query) {
851
847
}
852
848
853
849
getQueryHintsForCount ().forEach (query ::setHint );
850
+ applyComment (metadata , query ::setHint );
851
+ }
852
+
853
+ private Map <String , Object > getHints () {
854
+
855
+ Map <String , Object > hints = new HashMap <>();
856
+
857
+ getQueryHints ().withFetchGraphs (em ).forEach (hints ::put );
858
+
859
+ if (metadata != null ) {
860
+ applyComment (metadata , hints ::put );
861
+ }
862
+
863
+ return hints ;
864
+ }
865
+
866
+ private void applyComment (CrudMethodMetadata metadata , BiConsumer <String , Object > consumer ) {
854
867
855
868
if (metadata .getComment () != null && provider .getCommentHintKey () != null ) {
856
- query . setHint (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
869
+ consumer . accept (provider .getCommentHintKey (), provider .getCommentHintValue (this . metadata .getComment ()));
857
870
}
858
871
}
859
872
@@ -876,10 +889,6 @@ private static long executeCountQuery(TypedQuery<Long> query) {
876
889
return total ;
877
890
}
878
891
879
- private static boolean isUnpaged (Pageable pageable ) {
880
- return pageable .isUnpaged ();
881
- }
882
-
883
892
/**
884
893
* Specification that gives access to the {@link Parameter} instance used to bind the ids for
885
894
* {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses
0 commit comments