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 ;
@@ -230,9 +231,11 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
230
231
entityInformation .getIdAttribute ().getName ());
231
232
232
233
Query query = em .createQuery (queryString );
233
- /**
234
+
235
+ /*
234
236
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
235
237
*/
238
+
236
239
if (Collection .class .isInstance (ids )) {
237
240
query .setParameter ("ids" , ids );
238
241
} else {
@@ -304,33 +307,11 @@ public Optional<T> findById(ID id) {
304
307
}
305
308
306
309
LockModeType type = metadata .getLockModeType ();
307
-
308
- Map <String , Object > hints = new HashMap <>();
309
-
310
- getQueryHints ().withFetchGraphs (em ).forEach (hints ::put );
311
-
312
- if (metadata != null && metadata .getComment () != null && provider .getCommentHintKey () != null ) {
313
- hints .put (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
314
- }
310
+ Map <String , Object > hints = getHints ();
315
311
316
312
return Optional .ofNullable (type == null ? em .find (domainType , id , hints ) : em .find (domainType , id , type , hints ));
317
313
}
318
314
319
- /**
320
- * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
321
- * {@link EntityGraph} information.
322
- */
323
- protected QueryHints getQueryHints () {
324
- return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
325
- }
326
-
327
- /**
328
- * Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
329
- */
330
- protected QueryHints getQueryHintsForCount () {
331
- return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata ).forCounts ();
332
- }
333
-
334
315
@ Deprecated
335
316
@ Override
336
317
public T getOne (ID id ) {
@@ -437,7 +418,7 @@ public List<T> findAll(Sort sort) {
437
418
@ Override
438
419
public Page <T > findAll (Pageable pageable ) {
439
420
440
- if (isUnpaged (pageable )) {
421
+ if (pageable . isUnpaged ()) {
441
422
return new PageImpl <>(findAll ());
442
423
}
443
424
@@ -463,7 +444,7 @@ public List<T> findAll(Specification<T> spec) {
463
444
public Page <T > findAll (Specification <T > spec , Pageable pageable ) {
464
445
465
446
TypedQuery <T > query = getQuery (spec , pageable );
466
- return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
447
+ return pageable . isUnpaged () ? new PageImpl <>(query .getResultList ())
467
448
: readPage (query , getDomainClass (), pageable , spec );
468
449
}
469
450
@@ -475,9 +456,12 @@ public List<T> findAll(Specification<T> spec, Sort sort) {
475
456
@ Override
476
457
public boolean exists (Specification <T > spec ) {
477
458
478
- CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder ().createQuery (Integer .class );
479
- cq .select (this .em .getCriteriaBuilder ().literal (1 ));
459
+ CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder () //
460
+ .createQuery (Integer .class ) //
461
+ .select (this .em .getCriteriaBuilder ().literal (1 ));
462
+
480
463
applySpecificationToCriteria (spec , getDomainClass (), cq );
464
+
481
465
TypedQuery <Integer > query = applyRepositoryMethodMetadata (this .em .createQuery (cq ));
482
466
return query .setMaxResults (1 ).getResultList ().size () == 1 ;
483
467
}
@@ -565,9 +549,12 @@ public <S extends T> long count(Example<S> example) {
565
549
public <S extends T > boolean exists (Example <S > example ) {
566
550
567
551
Specification <S > spec = new ExampleSpecification <>(example , this .escapeCharacter );
568
- CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder ().createQuery (Integer .class );
569
- cq .select (this .em .getCriteriaBuilder ().literal (1 ));
552
+ CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder () //
553
+ .createQuery (Integer .class ) //
554
+ .select (this .em .getCriteriaBuilder ().literal (1 ));
555
+
570
556
applySpecificationToCriteria (spec , example .getProbeType (), cq );
557
+
571
558
TypedQuery <Integer > query = applyRepositoryMethodMetadata (this .em .createQuery (cq ));
572
559
return query .setMaxResults (1 ).getResultList ().size () == 1 ;
573
560
}
@@ -590,7 +577,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
590
577
Class <S > probeType = example .getProbeType ();
591
578
TypedQuery <S > query = getQuery (new ExampleSpecification <>(example , escapeCharacter ), probeType , pageable );
592
579
593
- return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ()) : readPage (query , probeType , pageable , spec );
580
+ return pageable . isUnpaged () ? new PageImpl <>(query .getResultList ()) : readPage (query , probeType , pageable , spec );
594
581
}
595
582
596
583
@ Override
@@ -804,6 +791,21 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
804
791
return applyRepositoryMethodMetadataForCount (em .createQuery (query ));
805
792
}
806
793
794
+ /**
795
+ * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
796
+ * {@link EntityGraph} information.
797
+ */
798
+ protected QueryHints getQueryHints () {
799
+ return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
800
+ }
801
+
802
+ /**
803
+ * Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
804
+ */
805
+ protected QueryHints getQueryHintsForCount () {
806
+ return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata ).forCounts ();
807
+ }
808
+
807
809
/**
808
810
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
809
811
*
@@ -854,10 +856,7 @@ private void applyQueryHints(Query query) {
854
856
}
855
857
856
858
getQueryHints ().withFetchGraphs (em ).forEach (query ::setHint );
857
-
858
- if (metadata .getComment () != null && provider .getCommentHintKey () != null ) {
859
- query .setHint (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
860
- }
859
+ applyComment (metadata , query ::setHint );
861
860
}
862
861
863
862
private <S > TypedQuery <S > applyRepositoryMethodMetadataForCount (TypedQuery <S > query ) {
@@ -878,9 +877,26 @@ private void applyQueryHintsForCount(Query query) {
878
877
}
879
878
880
879
getQueryHintsForCount ().forEach (query ::setHint );
880
+ applyComment (metadata , query ::setHint );
881
+ }
882
+
883
+ private Map <String , Object > getHints () {
884
+
885
+ Map <String , Object > hints = new HashMap <>();
886
+
887
+ getQueryHints ().withFetchGraphs (em ).forEach (hints ::put );
888
+
889
+ if (metadata != null ) {
890
+ applyComment (metadata , hints ::put );
891
+ }
892
+
893
+ return hints ;
894
+ }
895
+
896
+ private void applyComment (CrudMethodMetadata metadata , BiConsumer <String , Object > consumer ) {
881
897
882
898
if (metadata .getComment () != null && provider .getCommentHintKey () != null ) {
883
- query . setHint (provider .getCommentHintKey (), provider .getCommentHintValue (metadata .getComment ()));
899
+ consumer . accept (provider .getCommentHintKey (), provider .getCommentHintValue (this . metadata .getComment ()));
884
900
}
885
901
}
886
902
@@ -903,10 +919,6 @@ private static long executeCountQuery(TypedQuery<Long> query) {
903
919
return total ;
904
920
}
905
921
906
- private static boolean isUnpaged (Pageable pageable ) {
907
- return pageable .isUnpaged ();
908
- }
909
-
910
922
/**
911
923
* Specification that gives access to the {@link Parameter} instance used to bind the ids for
912
924
* {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses
0 commit comments