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 <>();
231+ // generate entity (proxies) without accessing the database.
235232 ids .forEach (id -> entities .add (getById (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 );
@@ -425,7 +422,7 @@ public List<T> findAllById(Iterable<ID> ids) {
425422
426423 if (entityInformation .hasCompositeId ()) {
427424
428- List <T > results = new ArrayList <T >();
425+ List <T > results = new ArrayList <>();
429426
430427 for (ID id : ids ) {
431428 findById (id ).ifPresent (results ::add );
@@ -436,7 +433,7 @@ public List<T> findAllById(Iterable<ID> ids) {
436433
437434 Collection <ID > idCollection = Streamable .of (ids ).toList ();
438435
439- ByIdsSpecification <T > specification = new ByIdsSpecification <T >(entityInformation );
436+ ByIdsSpecification <T > specification = new ByIdsSpecification <>(entityInformation );
440437 TypedQuery <T > query = getQuery (specification , Sort .unsorted ());
441438
442439 return query .setParameter (specification .parameter , idCollection ).getResultList ();
@@ -459,7 +456,7 @@ public List<T> findAll(Sort sort) {
459456 public Page <T > findAll (Pageable pageable ) {
460457
461458 if (isUnpaged (pageable )) {
462- return new PageImpl <T >(findAll ());
459+ return new PageImpl <>(findAll ());
463460 }
464461
465462 return findAll ((Specification <T >) null , pageable );
@@ -496,7 +493,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
496493 public Page <T > findAll (@ Nullable Specification <T > spec , Pageable pageable ) {
497494
498495 TypedQuery <T > query = getQuery (spec , pageable );
499- return isUnpaged (pageable ) ? new PageImpl <T >(query .getResultList ())
496+ return isUnpaged (pageable ) ? new PageImpl <>(query .getResultList ())
500497 : readPage (query , getDomainClass (), pageable , spec );
501498 }
502499
@@ -518,7 +515,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
518515
519516 try {
520517 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 ())
522519 .getSingleResult ());
523520 } catch (NoResultException e ) {
524521 return Optional .empty ();
@@ -532,7 +529,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
532529 @ Override
533530 public <S extends T > long count (Example <S > example ) {
534531 return executeCountQuery (
535- getCountQuery (new ExampleSpecification <S >(example , escapeCharacter ), example .getProbeType ()));
532+ getCountQuery (new ExampleSpecification <>(example , escapeCharacter ), example .getProbeType ()));
536533 }
537534
538535 /*
@@ -556,7 +553,7 @@ public <S extends T> boolean exists(Example<S> example) {
556553 */
557554 @ Override
558555 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 ())
560557 .getResultList ();
561558 }
562559
@@ -566,7 +563,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
566563 */
567564 @ Override
568565 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 )
570567 .getResultList ();
571568 }
572569
@@ -668,7 +665,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
668665
669666 Assert .notNull (entities , "Entities must not be null!" );
670667
671- List <S > result = new ArrayList <S >();
668+ List <S > result = new ArrayList <>();
672669
673670 for (S entity : entities ) {
674671 result .add (save (entity ));
@@ -708,7 +705,6 @@ public void flush() {
708705 * @param query must not be {@literal null}.
709706 * @param spec can be {@literal null}.
710707 * @param pageable must not be {@literal null}.
711- * @return
712708 * @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
713709 */
714710 @ Deprecated
@@ -724,7 +720,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
724720 * @param domainClass must not be {@literal null}.
725721 * @param spec can be {@literal null}.
726722 * @param pageable can be {@literal null}.
727- * @return
728723 */
729724 protected <S extends T > Page <S > readPage (TypedQuery <S > query , final Class <S > domainClass , Pageable pageable ,
730725 @ Nullable Specification <S > spec ) {
@@ -743,7 +738,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
743738 *
744739 * @param spec can be {@literal null}.
745740 * @param pageable must not be {@literal null}.
746- * @return
747741 */
748742 protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Pageable pageable ) {
749743
@@ -757,7 +751,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
757751 * @param spec can be {@literal null}.
758752 * @param domainClass must not be {@literal null}.
759753 * @param pageable must not be {@literal null}.
760- * @return
761754 */
762755 protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass ,
763756 Pageable pageable ) {
@@ -771,7 +764,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
771764 *
772765 * @param spec can be {@literal null}.
773766 * @param sort must not be {@literal null}.
774- * @return
775767 */
776768 protected TypedQuery <T > getQuery (@ Nullable Specification <T > spec , Sort sort ) {
777769 return getQuery (spec , getDomainClass (), sort );
@@ -783,7 +775,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
783775 * @param spec can be {@literal null}.
784776 * @param domainClass must not be {@literal null}.
785777 * @param sort must not be {@literal null}.
786- * @return
787778 */
788779 protected <S extends T > TypedQuery <S > getQuery (@ Nullable Specification <S > spec , Class <S > domainClass , Sort sort ) {
789780
@@ -804,7 +795,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
804795 * Creates a new count query for the given {@link Specification}.
805796 *
806797 * @param spec can be {@literal null}.
807- * @return
808798 * @deprecated override {@link #getCountQuery(Specification, Class)} instead
809799 */
810800 @ Deprecated
@@ -817,7 +807,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
817807 *
818808 * @param spec can be {@literal null}.
819809 * @param domainClass must not be {@literal null}.
820- * @return
821810 */
822811 protected <S extends T > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
823812
@@ -833,7 +822,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
833822 }
834823
835824 // Remove all Orders the Specifications might have applied
836- query .orderBy (Collections .< Order > emptyList ());
825+ query .orderBy (Collections .emptyList ());
837826
838827 return em .createQuery (query );
839828 }
@@ -844,7 +833,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
844833 * @param spec can be {@literal null}.
845834 * @param domainClass must not be {@literal null}.
846835 * @param query must not be {@literal null}.
847- * @return
848836 */
849837 private <S , U extends T > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
850838 CriteriaQuery <S > query ) {
@@ -890,7 +878,6 @@ private void applyQueryHints(Query query) {
890878 * Executes a count query and transparently sums up all values returned.
891879 *
892880 * @param query must not be {@literal null}.
893- * @return
894881 */
895882 private static long executeCountQuery (TypedQuery <Long > query ) {
896883
@@ -962,8 +949,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
962949 /**
963950 * Creates new {@link ExampleSpecification}.
964951 *
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}.
967954 */
968955 ExampleSpecification (Example <T > example , EscapeCharacter escapeCharacter ) {
969956
0 commit comments