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 <>();
235
- ids .forEach (id -> entities .add (getById (id )));
231
+ // generate entity (proxies) without accessing the database.
232
+ ids .forEach (id -> entities .add (getReferenceById (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 );
@@ -433,7 +430,7 @@ public List<T> findAllById(Iterable<ID> ids) {
433
430
434
431
if (entityInformation .hasCompositeId ()) {
435
432
436
- List <T > results = new ArrayList <T >();
433
+ List <T > results = new ArrayList <>();
437
434
438
435
for (ID id : ids ) {
439
436
findById (id ).ifPresent (results ::add );
@@ -444,7 +441,7 @@ public List<T> findAllById(Iterable<ID> ids) {
444
441
445
442
Collection <ID > idCollection = Streamable .of (ids ).toList ();
446
443
447
- ByIdsSpecification <T > specification = new ByIdsSpecification <T >(entityInformation );
444
+ ByIdsSpecification <T > specification = new ByIdsSpecification <>(entityInformation );
448
445
TypedQuery <T > query = getQuery (specification , Sort .unsorted ());
449
446
450
447
return query .setParameter (specification .parameter , idCollection ).getResultList ();
@@ -467,7 +464,7 @@ public List<T> findAll(Sort sort) {
467
464
public Page <T > findAll (Pageable pageable ) {
468
465
469
466
if (isUnpaged (pageable )) {
470
- return new PageImpl <T >(findAll ());
467
+ return new PageImpl <>(findAll ());
471
468
}
472
469
473
470
return findAll ((Specification <T >) null , pageable );
@@ -504,7 +501,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
504
501
public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
505
502
506
503
TypedQuery <T > query = getQuery (spec , pageable );
507
- return isUnpaged (pageable ) ? new PageImpl <T >(query .getResultList ())
504
+ return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
508
505
: readPage (query , getDomainClass (), pageable , spec );
509
506
}
510
507
@@ -526,7 +523,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
526
523
527
524
try {
528
525
return Optional
529
- .of (getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
526
+ .of (getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
530
527
.getSingleResult ());
531
528
} catch (NoResultException e ) {
532
529
return Optional .empty ();
@@ -540,7 +537,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
540
537
@ Override
541
538
public <S extends T > long count (Example <S > example ) {
542
539
return executeCountQuery (
543
- getCountQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType ()));
540
+ getCountQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType ()));
544
541
}
545
542
546
543
/*
@@ -564,7 +561,7 @@ public <S extends T> boolean exists(Example<S> example) {
564
561
*/
565
562
@ Override
566
563
public <S extends T > List <S > findAll (Example <S > example ) {
567
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
564
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
568
565
.getResultList ();
569
566
}
570
567
@@ -574,7 +571,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
574
571
*/
575
572
@ Override
576
573
public <S extends T > List <S > findAll (Example <S > example , Sort sort ) {
577
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), sort )
574
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), sort )
578
575
.getResultList ();
579
576
}
580
577
@@ -676,7 +673,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
676
673
677
674
Assert .notNull (entities , "Entities must not be null!" );
678
675
679
- List <S > result = new ArrayList <S >();
676
+ List <S > result = new ArrayList <>();
680
677
681
678
for (S entity : entities ) {
682
679
result .add (save (entity ));
@@ -716,7 +713,6 @@ public void flush() {
716
713
* @param query must not be {@literal null}.
717
714
* @param spec can be {@literal null}.
718
715
* @param pageable must not be {@literal null}.
719
- * @return
720
716
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
721
717
*/
722
718
@ Deprecated
@@ -732,7 +728,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
732
728
* @param domainClass must not be {@literal null}.
733
729
* @param spec can be {@literal null}.
734
730
* @param pageable can be {@literal null}.
735
- * @return
736
731
*/
737
732
protected <S extends T > Page <S > readPage (TypedQuery <S > query , final Class <S > domainClass , Pageable pageable ,
738
733
@ Nullable Specification <S > spec ) {
@@ -751,7 +746,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
751
746
*
752
747
* @param spec can be {@literal null}.
753
748
* @param pageable must not be {@literal null}.
754
- * @return
755
749
*/
756
750
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Pageable pageable ) {
757
751
@@ -765,7 +759,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
765
759
* @param spec can be {@literal null}.
766
760
* @param domainClass must not be {@literal null}.
767
761
* @param pageable must not be {@literal null}.
768
- * @return
769
762
*/
770
763
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass ,
771
764
Pageable pageable ) {
@@ -779,7 +772,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
779
772
*
780
773
* @param spec can be {@literal null}.
781
774
* @param sort must not be {@literal null}.
782
- * @return
783
775
*/
784
776
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Sort sort ) {
785
777
return getQuery (spec , getDomainClass (), sort );
@@ -791,7 +783,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
791
783
* @param spec can be {@literal null}.
792
784
* @param domainClass must not be {@literal null}.
793
785
* @param sort must not be {@literal null}.
794
- * @return
795
786
*/
796
787
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass , Sort sort ) {
797
788
@@ -812,7 +803,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
812
803
* Creates a new count query for the given {@link Specification}.
813
804
*
814
805
* @param spec can be {@literal null}.
815
- * @return
816
806
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
817
807
*/
818
808
@ Deprecated
@@ -825,7 +815,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
825
815
*
826
816
* @param spec can be {@literal null}.
827
817
* @param domainClass must not be {@literal null}.
828
- * @return
829
818
*/
830
819
protected <S extends T > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
831
820
@@ -841,7 +830,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
841
830
}
842
831
843
832
// Remove all Orders the Specifications might have applied
844
- query .orderBy (Collections .< Order > emptyList ());
833
+ query .orderBy (Collections .emptyList ());
845
834
846
835
return em .createQuery (query );
847
836
}
@@ -852,7 +841,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
852
841
* @param spec can be {@literal null}.
853
842
* @param domainClass must not be {@literal null}.
854
843
* @param query must not be {@literal null}.
855
- * @return
856
844
*/
857
845
private <S , U extends T > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
858
846
CriteriaQuery <S > query ) {
@@ -898,7 +886,6 @@ private void applyQueryHints(Query query) {
898
886
* Executes a count query and transparently sums up all values returned.
899
887
*
900
888
* @param query must not be {@literal null}.
901
- * @return
902
889
*/
903
890
private static long executeCountQuery (TypedQuery <Long > query ) {
904
891
@@ -970,8 +957,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
970
957
/**
971
958
* Creates new {@link ExampleSpecification}.
972
959
*
973
- * @param example
974
- * @param escapeCharacter
960
+ * @param example the example to base the specification of. Must not be {@literal null}.
961
+ * @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
975
962
*/
976
963
ExampleSpecification (Example <T > example , EscapeCharacter escapeCharacter ) {
977
964
0 commit comments