Skip to content

Commit

Permalink
#12 BikeRepository: remove @query as not compatible with Specification
Browse files Browse the repository at this point in the history
  • Loading branch information
bthreader committed Dec 23, 2023
1 parent 9560f5e commit 4d016db
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public class BikeEntitySpecification {
*/
public static Specification<BikeEntity> from(@NonNull BikesFilterInput input) {
return (root, query, cb) -> {
// Force fetch join
root.fetch("model", JoinType.INNER);
root.fetch("groupset", JoinType.INNER);
// Force fetch join on all queries apart from the count query used by JPA for paging
if (query.getResultType() != Long.class) {
root.fetch("model", JoinType.INNER);
root.fetch("groupset", JoinType.INNER);
}

List<Predicate> predicates = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package servicecourse.repo;

import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface BikeRepository extends JpaRepository<BikeEntity, Long>, JpaSpecificationExecutor<BikeEntity> {
@NonNull
@Query("SELECT b FROM BikeEntity b JOIN FETCH b.model JOIN FETCH b.groupset")
Page<BikeEntity> findAll(@NotNull Specification<BikeEntity> spec, @NotNull Pageable pageable);
}

0 comments on commit 4d016db

Please sign in to comment.