29
29
import org .springframework .data .jpa .domain .Specification ;
30
30
import org .springframework .data .jpa .provider .PersistenceProvider ;
31
31
import org .springframework .data .jpa .repository .EntityGraph ;
32
- import org .springframework .data .jpa .repository .query .DefaultQueryEnhancer ;
33
32
import org .springframework .data .jpa .repository .query .EscapeCharacter ;
34
33
import org .springframework .data .jpa .repository .query .QueryUtils ;
35
34
import org .springframework .data .jpa .repository .support .QueryHints .NoHints ;
61
60
* @author Jesse Wouters
62
61
* @author Greg Turnquist
63
62
* @author Yanming Zhou
63
+ * @author Ernst-Jan van der Laan
64
64
*/
65
65
@ Repository
66
66
@ Transactional (readOnly = true )
@@ -206,13 +206,22 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
206
206
return ;
207
207
}
208
208
209
- String queryString = String .format (DELETE_ALL_QUERY_BY_ID_STRING , entityInformation .getEntityName (),
210
- entityInformation .getIdAttribute ().getName ());
209
+ if (entityInformation .hasCompositeId ()) {
210
+
211
+ List <T > entities = new ArrayList <>();
212
+ // generate entity (proxies) without accessing the database.
213
+ ids .forEach (id -> entities .add (getReferenceById (id )));
214
+ deleteAllInBatch (entities );
215
+ } else {
211
216
212
- Query query = em . createQuery ( queryString );
213
- query . setParameter ( "ids" , ids );
217
+ String queryString = String . format ( DELETE_ALL_QUERY_BY_ID_STRING , entityInformation . getEntityName (),
218
+ entityInformation . getIdAttribute (). getName () );
214
219
215
- query .executeUpdate ();
220
+ Query query = em .createQuery (queryString );
221
+ query .setParameter ("ids" , ids );
222
+
223
+ query .executeUpdate ();
224
+ }
216
225
}
217
226
218
227
/*
@@ -298,7 +307,6 @@ public Optional<T> findById(ID id) {
298
307
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
299
308
* {@link EntityGraph} information.
300
309
*
301
- * @return
302
310
*/
303
311
protected QueryHints getQueryHints () {
304
312
return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
@@ -403,7 +411,7 @@ public List<T> findAllById(Iterable<ID> ids) {
403
411
404
412
if (entityInformation .hasCompositeId ()) {
405
413
406
- List <T > results = new ArrayList <T >();
414
+ List <T > results = new ArrayList <>();
407
415
408
416
for (ID id : ids ) {
409
417
findById (id ).ifPresent (results ::add );
@@ -414,7 +422,7 @@ public List<T> findAllById(Iterable<ID> ids) {
414
422
415
423
Collection <ID > idCollection = Streamable .of (ids ).toList ();
416
424
417
- ByIdsSpecification <T > specification = new ByIdsSpecification <T >(entityInformation );
425
+ ByIdsSpecification <T > specification = new ByIdsSpecification <>(entityInformation );
418
426
TypedQuery <T > query = getQuery (specification , Sort .unsorted ());
419
427
420
428
return query .setParameter (specification .parameter , idCollection ).getResultList ();
@@ -437,7 +445,7 @@ public List<T> findAll(Sort sort) {
437
445
public Page <T > findAll (Pageable pageable ) {
438
446
439
447
if (isUnpaged (pageable )) {
440
- return new PageImpl <T >(findAll ());
448
+ return new PageImpl <>(findAll ());
441
449
}
442
450
443
451
return findAll ((Specification <T >) null , pageable );
@@ -474,7 +482,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
474
482
public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
475
483
476
484
TypedQuery <T > query = getQuery (spec , pageable );
477
- return isUnpaged (pageable ) ? new PageImpl <T >(query .getResultList ())
485
+ return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
478
486
: readPage (query , getDomainClass (), pageable , spec );
479
487
}
480
488
@@ -496,7 +504,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
496
504
497
505
try {
498
506
return Optional
499
- .of (getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
507
+ .of (getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
500
508
.getSingleResult ());
501
509
} catch (NoResultException e ) {
502
510
return Optional .empty ();
@@ -510,7 +518,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
510
518
@ Override
511
519
public <S extends T > long count (Example <S > example ) {
512
520
return executeCountQuery (
513
- getCountQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType ()));
521
+ getCountQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType ()));
514
522
}
515
523
516
524
/*
@@ -534,7 +542,7 @@ public <S extends T> boolean exists(Example<S> example) {
534
542
*/
535
543
@ Override
536
544
public <S extends T > List <S > findAll (Example <S > example ) {
537
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
545
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), Sort .unsorted ())
538
546
.getResultList ();
539
547
}
540
548
@@ -544,8 +552,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
544
552
*/
545
553
@ Override
546
554
public <S extends T > List <S > findAll (Example <S > example , Sort sort ) {
547
- return getQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType (), sort )
548
- .getResultList ();
555
+ return getQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType (), sort ).getResultList ();
549
556
}
550
557
551
558
/*
@@ -646,7 +653,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
646
653
647
654
Assert .notNull (entities , "Entities must not be null!" );
648
655
649
- List <S > result = new ArrayList <S >();
656
+ List <S > result = new ArrayList <>();
650
657
651
658
for (S entity : entities ) {
652
659
result .add (save (entity ));
@@ -686,7 +693,6 @@ public void flush() {
686
693
* @param query must not be {@literal null}.
687
694
* @param spec can be {@literal null}.
688
695
* @param pageable must not be {@literal null}.
689
- * @return
690
696
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
691
697
*/
692
698
@ Deprecated
@@ -702,7 +708,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
702
708
* @param domainClass must not be {@literal null}.
703
709
* @param spec can be {@literal null}.
704
710
* @param pageable can be {@literal null}.
705
- * @return
706
711
*/
707
712
protected <S extends T > Page <S > readPage (TypedQuery <S > query , final Class <S > domainClass , Pageable pageable ,
708
713
@ Nullable Specification <S > spec ) {
@@ -721,7 +726,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
721
726
*
722
727
* @param spec can be {@literal null}.
723
728
* @param pageable must not be {@literal null}.
724
- * @return
725
729
*/
726
730
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Pageable pageable ) {
727
731
@@ -735,7 +739,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
735
739
* @param spec can be {@literal null}.
736
740
* @param domainClass must not be {@literal null}.
737
741
* @param pageable must not be {@literal null}.
738
- * @return
739
742
*/
740
743
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass ,
741
744
Pageable pageable ) {
@@ -749,7 +752,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
749
752
*
750
753
* @param spec can be {@literal null}.
751
754
* @param sort must not be {@literal null}.
752
- * @return
753
755
*/
754
756
protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Sort sort ) {
755
757
return getQuery (spec , getDomainClass (), sort );
@@ -761,7 +763,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
761
763
* @param spec can be {@literal null}.
762
764
* @param domainClass must not be {@literal null}.
763
765
* @param sort must not be {@literal null}.
764
- * @return
765
766
*/
766
767
protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass , Sort sort ) {
767
768
@@ -782,7 +783,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
782
783
* Creates a new count query for the given {@link Specification}.
783
784
*
784
785
* @param spec can be {@literal null}.
785
- * @return
786
786
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
787
787
*/
788
788
@ Deprecated
@@ -795,7 +795,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
795
795
*
796
796
* @param spec can be {@literal null}.
797
797
* @param domainClass must not be {@literal null}.
798
- * @return
799
798
*/
800
799
protected <S extends T > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
801
800
@@ -811,7 +810,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
811
810
}
812
811
813
812
// Remove all Orders the Specifications might have applied
814
- query .orderBy (Collections .< Order > emptyList ());
813
+ query .orderBy (Collections .emptyList ());
815
814
816
815
return em .createQuery (query );
817
816
}
@@ -822,7 +821,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
822
821
* @param spec can be {@literal null}.
823
822
* @param domainClass must not be {@literal null}.
824
823
* @param query must not be {@literal null}.
825
- * @return
826
824
*/
827
825
private <S , U extends T > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
828
826
CriteriaQuery <S > query ) {
@@ -868,7 +866,6 @@ private void applyQueryHints(Query query) {
868
866
* Executes a count query and transparently sums up all values returned.
869
867
*
870
868
* @param query must not be {@literal null}.
871
- * @return
872
869
*/
873
870
private static long executeCountQuery (TypedQuery <Long > query ) {
874
871
@@ -941,8 +938,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
941
938
/**
942
939
* Creates new {@link ExampleSpecification}.
943
940
*
944
- * @param example
945
- * @param escapeCharacter
941
+ * @param example the example to base the specification of. Must not be {@literal null}.
942
+ * @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
946
943
*/
947
944
ExampleSpecification (Example <T > example , EscapeCharacter escapeCharacter ) {
948
945
0 commit comments