From 09c44d46126264f6aa951fa13f988e630ade0259 Mon Sep 17 00:00:00 2001 From: fladdimir Date: Wed, 31 Jul 2024 23:58:30 +0200 Subject: [PATCH] return List instead of Iterable and adapt to new spring-data-jpa interface-hierarchy --- .../repository/EntityGraphCrudRepository.java | 15 ++++++++------- .../repository/EntityGraphJpaRepository.java | 1 + ...EntityGraphPagingAndSortingRepository.java | 11 ++++++----- .../EntityGraphQueryByExampleExecutor.java | 12 ++++++------ .../EntityGraphSimpleJpaRepository.java | 4 ++-- .../EntityGraphJpaRepositoryTest.java | 19 ++++++++++++++++++- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphCrudRepository.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphCrudRepository.java index 50102af..3feabba 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphCrudRepository.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphCrudRepository.java @@ -1,8 +1,9 @@ package com.cosium.spring.data.jpa.entity.graph.repository; import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph; +import java.util.List; import java.util.Optional; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.ListCrudRepository; import org.springframework.data.repository.NoRepositoryBean; /** @@ -12,20 +13,20 @@ */ @NoRepositoryBean public interface EntityGraphCrudRepository - extends CrudRepository, EntityGraphRepository { + extends ListCrudRepository, EntityGraphRepository { /** - * @see CrudRepository#findById(Object) + * @see ListCrudRepository#findById(Object) */ Optional findById(ID id, EntityGraph entityGraph); /** - * @see CrudRepository#findAllById(Iterable) + * @see ListCrudRepository#findAllById(Iterable) */ - Iterable findAllById(Iterable ids, EntityGraph entityGraph); + List findAllById(Iterable ids, EntityGraph entityGraph); /** - * @see CrudRepository#findAll() + * @see ListCrudRepository#findAll() */ - Iterable findAll(EntityGraph entityGraph); + List findAll(EntityGraph entityGraph); } diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepository.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepository.java index 45f4397..cb713e9 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepository.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepository.java @@ -11,5 +11,6 @@ @NoRepositoryBean public interface EntityGraphJpaRepository extends JpaRepository, + EntityGraphCrudRepository, EntityGraphPagingAndSortingRepository, EntityGraphQueryByExampleExecutor {} diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphPagingAndSortingRepository.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphPagingAndSortingRepository.java index b6ada27..67e4bc4 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphPagingAndSortingRepository.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphPagingAndSortingRepository.java @@ -1,11 +1,12 @@ package com.cosium.spring.data.jpa.entity.graph.repository; import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph; +import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.repository.ListPagingAndSortingRepository; import org.springframework.data.repository.NoRepositoryBean; -import org.springframework.data.repository.PagingAndSortingRepository; /** * Created on 18/03/17. @@ -14,15 +15,15 @@ */ @NoRepositoryBean public interface EntityGraphPagingAndSortingRepository - extends PagingAndSortingRepository, EntityGraphCrudRepository { + extends ListPagingAndSortingRepository, EntityGraphRepository { /** - * @see PagingAndSortingRepository#findAll(Pageable) + * @see ListPagingAndSortingRepository#findAll(Pageable) */ Page findAll(Pageable pageable, EntityGraph entityGraph); /** - * @see PagingAndSortingRepository#findAll(Sort) + * @see ListPagingAndSortingRepository#findAll(Sort) */ - Iterable findAll(Sort sort, EntityGraph entityGraph); + List findAll(Sort sort, EntityGraph entityGraph); } diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphQueryByExampleExecutor.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphQueryByExampleExecutor.java index 9c60bd4..14fe8e6 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphQueryByExampleExecutor.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphQueryByExampleExecutor.java @@ -8,7 +8,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.repository.NoRepositoryBean; -import org.springframework.data.repository.query.QueryByExampleExecutor; +import org.springframework.data.repository.query.ListQueryByExampleExecutor; /** * Created on 18/03/17. @@ -16,25 +16,25 @@ * @author Reda.Housni-Alaoui */ @NoRepositoryBean -public interface EntityGraphQueryByExampleExecutor extends QueryByExampleExecutor { +public interface EntityGraphQueryByExampleExecutor extends ListQueryByExampleExecutor { /** - * @see QueryByExampleExecutor#findAll(Example, Pageable) + * @see ListQueryByExampleExecutor#findAll(Example, Pageable) */ Page findAll(Example example, Pageable pageable, EntityGraph entityGraph); /** - * @see QueryByExampleExecutor#findOne(Example) + * @see ListQueryByExampleExecutor#findOne(Example) */ Optional findOne(Example example, EntityGraph entityGraph); /** - * @see QueryByExampleExecutor#findAll(Example, Sort) + * @see ListQueryByExampleExecutor#findAll(Example, Sort) */ List findAll(Example example, Sort sort, EntityGraph entityGraph); /** - * @see QueryByExampleExecutor#findAll(Example) + * @see ListQueryByExampleExecutor#findAll(Example) */ List findAll(Example example, EntityGraph entityGraph); } diff --git a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/support/EntityGraphSimpleJpaRepository.java b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/support/EntityGraphSimpleJpaRepository.java index f0b9dc8..1247373 100644 --- a/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/support/EntityGraphSimpleJpaRepository.java +++ b/core/src/main/java/com/cosium/spring/data/jpa/entity/graph/repository/support/EntityGraphSimpleJpaRepository.java @@ -90,12 +90,12 @@ public List findAllById(Iterable ids, EntityGraph entityGraph) { } @Override - public Iterable findAll(Sort sort, EntityGraph entityGraph) { + public List findAll(Sort sort, EntityGraph entityGraph) { return findAll(sort); } @Override - public Iterable findAll(EntityGraph entityGraph) { + public List findAll(EntityGraph entityGraph) { return findAll(); } } diff --git a/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepositoryTest.java b/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepositoryTest.java index 5c2c200..42bfa8a 100644 --- a/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepositoryTest.java +++ b/core/src/test/java/com/cosium/spring/data/jpa/entity/graph/repository/EntityGraphJpaRepositoryTest.java @@ -10,6 +10,7 @@ import com.cosium.spring.data.jpa.entity.graph.domain2.NamedEntityGraph; import com.cosium.spring.data.jpa.entity.graph.repository.exception.InapplicableEntityGraphException; import com.cosium.spring.data.jpa.entity.graph.sample.Brand; +import com.cosium.spring.data.jpa.entity.graph.sample.BrandEntityGraph; import com.cosium.spring.data.jpa.entity.graph.sample.Product; import com.cosium.spring.data.jpa.entity.graph.sample.ProductEntityGraph; import com.github.springtestdbunit.annotation.DatabaseSetup; @@ -331,6 +332,22 @@ void test23() { assertThat(productName).isNotNull(); } + @Transactional + @Test + @DisplayName( + "Given an EntityGraphJpaRepository when calling a standard findAll method then a List should be returned (no matter whether an entitygraph is provided or not)") + void test24() { + long brandId = 1; + + List brandsWithoutEntityGraph = brandRepository.findAllById(List.of(brandId)); + assertThat(brandsWithoutEntityGraph).hasSize(1); + + List brandsWithEntityGraph = + brandRepository.findAllById( + List.of(brandId), BrandEntityGraph.____().products().____.____()); + assertThat(brandsWithEntityGraph).hasSize(1); + } + private abstract static class EntityGraphSpecification implements Specification, EntityGraph { @@ -355,7 +372,7 @@ public interface ProductRepository @org.springframework.data.jpa.repository.EntityGraph(attributePaths = "brand") @Override - Iterable findAll(EntityGraph entityGraph); + List findAll(EntityGraph entityGraph); @org.springframework.data.jpa.repository.EntityGraph(attributePaths = "brand") List findByName(String name, EntityGraph entityGraph);