15
15
*/
16
16
package org .springframework .data .jpa .repository .support ;
17
17
18
- import static org .springframework .data .jpa .repository .query .QueryUtils .*;
19
-
20
- import java .util .ArrayList ;
21
- import java .util .Collection ;
22
- import java .util .Collections ;
23
- import java .util .HashMap ;
24
- import java .util .List ;
25
- import java .util .Map ;
26
- import java .util .Optional ;
27
- import java .util .function .Function ;
28
-
29
- import javax .persistence .EntityManager ;
30
- import javax .persistence .LockModeType ;
31
- import javax .persistence .NoResultException ;
32
- import javax .persistence .Parameter ;
33
- import javax .persistence .Query ;
34
- import javax .persistence .TypedQuery ;
35
- import javax .persistence .criteria .CriteriaBuilder ;
36
- import javax .persistence .criteria .CriteriaQuery ;
37
- import javax .persistence .criteria .Order ;
38
- import javax .persistence .criteria .ParameterExpression ;
39
- import javax .persistence .criteria .Path ;
40
- import javax .persistence .criteria .Predicate ;
41
- import javax .persistence .criteria .Root ;
42
-
43
18
import org .springframework .dao .EmptyResultDataAccessException ;
44
19
import org .springframework .data .domain .Example ;
45
20
import org .springframework .data .domain .Page ;
62
37
import org .springframework .transaction .annotation .Transactional ;
63
38
import org .springframework .util .Assert ;
64
39
40
+ import javax .persistence .EntityManager ;
41
+ import javax .persistence .LockModeType ;
42
+ import javax .persistence .NoResultException ;
43
+ import javax .persistence .Parameter ;
44
+ import javax .persistence .Query ;
45
+ import javax .persistence .TypedQuery ;
46
+ import javax .persistence .criteria .CriteriaBuilder ;
47
+ import javax .persistence .criteria .CriteriaQuery ;
48
+ import javax .persistence .criteria .ParameterExpression ;
49
+ import javax .persistence .criteria .Path ;
50
+ import javax .persistence .criteria .Predicate ;
51
+ import javax .persistence .criteria .Root ;
52
+ import java .util .ArrayList ;
53
+ import java .util .Collection ;
54
+ import java .util .Collections ;
55
+ import java .util .HashMap ;
56
+ import java .util .List ;
57
+ import java .util .Map ;
58
+ import java .util .Optional ;
59
+ import java .util .function .Function ;
60
+
61
+ import static org .springframework .data .jpa .repository .query .QueryUtils .*;
62
+
65
63
/**
66
64
* Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
67
65
* you a more sophisticated interface than the plain {@link EntityManager} .
@@ -228,13 +226,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
228
226
}
229
227
230
228
if (entityInformation .hasCompositeId ()) {
231
- // XXX Hibernate just creates an empty Entity when doing the getById.
232
- // Others might do a select right away causing a big performance penalty.
233
- // See JavaDoc for getById.
229
+
234
230
List <T > entities = new ArrayList <>();
231
+ // generate entity (proxies) without accessing the database.
235
232
ids .forEach (id -> entities .add (getById (id )));
236
233
deleteAllInBatch (entities );
237
234
} else {
235
+
238
236
String queryString = String .format (DELETE_ALL_QUERY_BY_ID_STRING , entityInformation .getEntityName (),
239
237
entityInformation .getIdAttribute ().getName ());
240
238
@@ -328,7 +326,6 @@ public Optional<T> findById(ID id) {
328
326
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
329
327
* {@link EntityGraph} information.
330
328
*
331
- * @return
332
329
*/
333
330
protected QueryHints getQueryHints () {
334
331
return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
@@ -425,7 +422,7 @@ public List<T> findAllById(Iterable<ID> ids) {
425
422
426
423
if (entityInformation .hasCompositeId ()) {
427
424
428
- List <T > results = new ArrayList <T >();
425
+ List <T > results = new ArrayList <>();
429
426
430
427
for (ID id : ids ) {
431
428
findById (id ).ifPresent (results ::add );
@@ -436,7 +433,7 @@ public List<T> findAllById(Iterable<ID> ids) {
436
433
437
434
Collection <ID > idCollection = Streamable .of (ids ).toList ();
438
435
439
- ByIdsSpecification <T > specification = new ByIdsSpecification <T >(entityInformation );
436
+ ByIdsSpecification <T > specification = new ByIdsSpecification <>(entityInformation );
440
437
TypedQuery <T > query = getQuery (specification , Sort .unsorted ());
441
438
442
439
return query .setParameter (specification .parameter , idCollection ).getResultList ();
@@ -459,7 +456,7 @@ public List<T> findAll(Sort sort) {
459
456
public Page <T > findAll (Pageable pageable ) {
460
457
461
458
if (isUnpaged (pageable )) {
462
- return new PageImpl <T >(findAll ());
459
+ return new PageImpl <>(findAll ());
463
460
}
464
461
465
462
return findAll ((Specification <T >) null , pageable );
@@ -496,7 +493,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
496
493
public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
497
494
498
495
TypedQuery <T > query = getQuery (spec , pageable );
499
- return isUnpaged (pageable ) ? new PageImpl <T >(query .getResultList ())
496
+ return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
500
497
: readPage (query , getDomainClass (), pageable , spec );
501
498
}
502
499
@@ -518,7 +515,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
518
515
519
516
try {
520
517
return Optional
521
- .of (getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
518
+ .of (getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
522
519
.getSingleResult ());
523
520
} catch (NoResultException e ) {
524
521
return Optional .empty ();
@@ -532,7 +529,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
532
529
@ Override
533
530
public <S extends T > long count (Example <S > example ) {
534
531
return executeCountQuery (
535
- getCountQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType ()));
532
+ getCountQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType ()));
536
533
}
537
534
538
535
/*
@@ -556,7 +553,7 @@ public <S extends T> boolean exists(Example<S> example) {
556
553
*/
557
554
@ Override
558
555
public <S extends T > List <S > findAll (Example <S > example ) {
559
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
556
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
560
557
.getResultList ();
561
558
}
562
559
@@ -566,7 +563,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
566
563
*/
567
564
@ Override
568
565
public <S extends T > List <S > findAll (Example <S > example , Sort sort ) {
569
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), sort )
566
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), sort )
570
567
.getResultList ();
571
568
}
572
569
@@ -668,7 +665,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
668
665
669
666
Assert .notNull (entities , "Entities must not be null!" );
670
667
671
- List <S > result = new ArrayList <S >();
668
+ List <S > result = new ArrayList <>();
672
669
673
670
for (S entity : entities ) {
674
671
result .add (save (entity ));
@@ -708,7 +705,6 @@ public void flush() {
708
705
* @param query must not be {@literal null}.
709
706
* @param spec can be {@literal null}.
710
707
* @param pageable must not be {@literal null}.
711
- * @return
712
708
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
713
709
*/
714
710
@ Deprecated
@@ -724,7 +720,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
724
720
* @param domainClass must not be {@literal null}.
725
721
* @param spec can be {@literal null}.
726
722
* @param pageable can be {@literal null}.
727
- * @return
728
723
*/
729
724
protected <S extends T > Page <S > readPage (TypedQuery <S > query , final Class <S > domainClass , Pageable pageable ,
730
725
@ Nullable Specification <S > spec ) {
@@ -743,7 +738,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
743
738
*
744
739
* @param spec can be {@literal null}.
745
740
* @param pageable must not be {@literal null}.
746
- * @return
747
741
*/
748
742
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Pageable pageable ) {
749
743
@@ -757,7 +751,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
757
751
* @param spec can be {@literal null}.
758
752
* @param domainClass must not be {@literal null}.
759
753
* @param pageable must not be {@literal null}.
760
- * @return
761
754
*/
762
755
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass ,
763
756
Pageable pageable ) {
@@ -771,7 +764,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
771
764
*
772
765
* @param spec can be {@literal null}.
773
766
* @param sort must not be {@literal null}.
774
- * @return
775
767
*/
776
768
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Sort sort ) {
777
769
return getQuery (spec , getDomainClass (), sort );
@@ -783,7 +775,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
783
775
* @param spec can be {@literal null}.
784
776
* @param domainClass must not be {@literal null}.
785
777
* @param sort must not be {@literal null}.
786
- * @return
787
778
*/
788
779
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass , Sort sort ) {
789
780
@@ -804,7 +795,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
804
795
* Creates a new count query for the given {@link Specification}.
805
796
*
806
797
* @param spec can be {@literal null}.
807
- * @return
808
798
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
809
799
*/
810
800
@ Deprecated
@@ -817,7 +807,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
817
807
*
818
808
* @param spec can be {@literal null}.
819
809
* @param domainClass must not be {@literal null}.
820
- * @return
821
810
*/
822
811
protected <S extends T > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
823
812
@@ -833,7 +822,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
833
822
}
834
823
835
824
// Remove all Orders the Specifications might have applied
836
- query .orderBy (Collections .< Order > emptyList ());
825
+ query .orderBy (Collections .emptyList ());
837
826
838
827
return em .createQuery (query );
839
828
}
@@ -844,7 +833,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
844
833
* @param spec can be {@literal null}.
845
834
* @param domainClass must not be {@literal null}.
846
835
* @param query must not be {@literal null}.
847
- * @return
848
836
*/
849
837
private <S , U extends T > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
850
838
CriteriaQuery <S > query ) {
@@ -890,7 +878,6 @@ private void applyQueryHints(Query query) {
890
878
* Executes a count query and transparently sums up all values returned.
891
879
*
892
880
* @param query must not be {@literal null}.
893
- * @return
894
881
*/
895
882
private static long executeCountQuery (TypedQuery <Long > query ) {
896
883
@@ -962,8 +949,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
962
949
/**
963
950
* Creates new {@link ExampleSpecification}.
964
951
*
965
- * @param example
966
- * @param escapeCharacter
952
+ * @param example the example to base the specification of. Must not be {@literal null}.
953
+ * @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
967
954
*/
968
955
ExampleSpecification (Example <T > example , EscapeCharacter escapeCharacter ) {
969
956
0 commit comments