1515 */
1616package org .springframework .data .jpa .repository .support ;
1717
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-
4318import org .springframework .dao .EmptyResultDataAccessException ;
4419import org .springframework .data .domain .Example ;
4520import org .springframework .data .domain .Page ;
6237import org .springframework .transaction .annotation .Transactional ;
6338import org .springframework .util .Assert ;
6439
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+
6563/**
6664 * Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
6765 * you a more sophisticated interface than the plain {@link EntityManager} .
@@ -228,13 +226,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
228226 }
229227
230228 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+
234230 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 )));
236233 deleteAllInBatch (entities );
237234 } else {
235+
238236 String queryString = String .format (DELETE_ALL_QUERY_BY_ID_STRING , entityInformation .getEntityName (),
239237 entityInformation .getIdAttribute ().getName ());
240238
@@ -328,7 +326,6 @@ public Optional<T> findById(ID id) {
328326 * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
329327 * {@link EntityGraph} information.
330328 *
331- * @return
332329 */
333330 protected QueryHints getQueryHints () {
334331 return metadata == null ? NoHints .INSTANCE : DefaultQueryHints .of (entityInformation , metadata );
@@ -433,7 +430,7 @@ public List<T> findAllById(Iterable<ID> ids) {
433430
434431 if (entityInformation .hasCompositeId ()) {
435432
436- List <T > results = new ArrayList <T >();
433+ List <T > results = new ArrayList <>();
437434
438435 for (ID id : ids ) {
439436 findById (id ).ifPresent (results ::add );
@@ -444,7 +441,7 @@ public List<T> findAllById(Iterable<ID> ids) {
444441
445442 Collection <ID > idCollection = Streamable .of (ids ).toList ();
446443
447- ByIdsSpecification <T > specification = new ByIdsSpecification <T >(entityInformation );
444+ ByIdsSpecification <T > specification = new ByIdsSpecification <>(entityInformation );
448445 TypedQuery <T > query = getQuery (specification , Sort .unsorted ());
449446
450447 return query .setParameter (specification .parameter , idCollection ).getResultList ();
@@ -467,7 +464,7 @@ public List<T> findAll(Sort sort) {
467464 public Page <T > findAll (Pageable pageable ) {
468465
469466 if (isUnpaged (pageable )) {
470- return new PageImpl <T >(findAll ());
467+ return new PageImpl <>(findAll ());
471468 }
472469
473470 return findAll ((Specification <T >) null , pageable );
@@ -504,7 +501,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
504501 public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
505502
506503 TypedQuery <T > query = getQuery (spec , pageable );
507- return isUnpaged (pageable ) ? new PageImpl <T >(query .getResultList ())
504+ return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
508505 : readPage (query , getDomainClass (), pageable , spec );
509506 }
510507
@@ -526,7 +523,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
526523
527524 try {
528525 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 ())
530527 .getSingleResult ());
531528 } catch (NoResultException e ) {
532529 return Optional .empty ();
@@ -540,7 +537,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
540537 @ Override
541538 public <S extends T > long count (Example <S > example ) {
542539 return executeCountQuery (
543- getCountQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType ()));
540+ getCountQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType ()));
544541 }
545542
546543 /*
@@ -564,7 +561,7 @@ public <S extends T> boolean exists(Example<S> example) {
564561 */
565562 @ Override
566563 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 ())
568565 .getResultList ();
569566 }
570567
@@ -574,7 +571,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
574571 */
575572 @ Override
576573 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 )
578575 .getResultList ();
579576 }
580577
@@ -676,7 +673,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
676673
677674 Assert .notNull (entities , "Entities must not be null!" );
678675
679- List <S > result = new ArrayList <S >();
676+ List <S > result = new ArrayList <>();
680677
681678 for (S entity : entities ) {
682679 result .add (save (entity ));
@@ -716,7 +713,6 @@ public void flush() {
716713 * @param query must not be {@literal null}.
717714 * @param spec can be {@literal null}.
718715 * @param pageable must not be {@literal null}.
719- * @return
720716 * @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
721717 */
722718 @ Deprecated
@@ -732,7 +728,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
732728 * @param domainClass must not be {@literal null}.
733729 * @param spec can be {@literal null}.
734730 * @param pageable can be {@literal null}.
735- * @return
736731 */
737732 protected <S extends T > Page <S > readPage (TypedQuery <S > query , final Class <S > domainClass , Pageable pageable ,
738733 @ Nullable Specification <S > spec ) {
@@ -751,7 +746,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
751746 *
752747 * @param spec can be {@literal null}.
753748 * @param pageable must not be {@literal null}.
754- * @return
755749 */
756750 protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Pageable pageable ) {
757751
@@ -765,7 +759,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
765759 * @param spec can be {@literal null}.
766760 * @param domainClass must not be {@literal null}.
767761 * @param pageable must not be {@literal null}.
768- * @return
769762 */
770763 protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass ,
771764 Pageable pageable ) {
@@ -779,7 +772,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
779772 *
780773 * @param spec can be {@literal null}.
781774 * @param sort must not be {@literal null}.
782- * @return
783775 */
784776 protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Sort sort ) {
785777 return getQuery (spec , getDomainClass (), sort );
@@ -791,7 +783,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
791783 * @param spec can be {@literal null}.
792784 * @param domainClass must not be {@literal null}.
793785 * @param sort must not be {@literal null}.
794- * @return
795786 */
796787 protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass , Sort sort ) {
797788
@@ -812,7 +803,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
812803 * Creates a new count query for the given {@link Specification}.
813804 *
814805 * @param spec can be {@literal null}.
815- * @return
816806 * @deprecated override {@link #getCountQuery(Specification, Class)} instead
817807 */
818808 @ Deprecated
@@ -825,7 +815,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
825815 *
826816 * @param spec can be {@literal null}.
827817 * @param domainClass must not be {@literal null}.
828- * @return
829818 */
830819 protected <S extends T > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
831820
@@ -841,7 +830,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
841830 }
842831
843832 // Remove all Orders the Specifications might have applied
844- query .orderBy (Collections .< Order > emptyList ());
833+ query .orderBy (Collections .emptyList ());
845834
846835 return em .createQuery (query );
847836 }
@@ -852,7 +841,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
852841 * @param spec can be {@literal null}.
853842 * @param domainClass must not be {@literal null}.
854843 * @param query must not be {@literal null}.
855- * @return
856844 */
857845 private <S , U extends T > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
858846 CriteriaQuery <S > query ) {
@@ -898,7 +886,6 @@ private void applyQueryHints(Query query) {
898886 * Executes a count query and transparently sums up all values returned.
899887 *
900888 * @param query must not be {@literal null}.
901- * @return
902889 */
903890 private static long executeCountQuery (TypedQuery <Long > query ) {
904891
@@ -970,8 +957,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
970957 /**
971958 * Creates new {@link ExampleSpecification}.
972959 *
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}.
975962 */
976963 ExampleSpecification (Example <T > example , EscapeCharacter escapeCharacter ) {
977964
0 commit comments