如何在JpaRepository中定义自定义findAll方法?

时间:2020-06-29 11:43:20

标签: java spring-boot jpa spring-data-jpa entitygraph

我有两个api端点:

  • /api/posts-获取分页帖子列表
  • /api/countries/{country}/posts-按国家/地区获取分页帖子列表

所以我有以下实体:

@Data
@Entity
@Table(name = "posts")
@EntityListeners(AuditingEntityListener.class)
@NamedEntityGraph(
    name = "Post.Resource",
    attributeNodes = @NamedAttributeNode("country")
)
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String title;
    private String body;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "country_id")
    private Country country;

    @Column(name = "published_at")
    private LocalDateTime publishedAt;

    @CreatedDate
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    public boolean isPublished() {
        return publishedAt != null;
    }

    public boolean isDraft() {
        return !isPublished();
    }
}

我已经定义了以下存储库。请注意,我是如何为后两种方法定义实体图的,所以我需要这样做,因为我不想覆盖findAll()方法,因为将来我将需要加载没有任何关系的帖子。我还希望它是谓词,以便在各种不同的服务中我可以重用方法,而不是为每个查询创建方法...

@Repository
public interface PostRepository extends JpaRepository<Post, Long>, 
    JpaSpecificationExecutor<Post>, 
    QuerydslPredicateExecutor<Post> 
{
    @EntityGraph("Post.Resource")
    Optional<Post> findPostResourceById(long id);

    @Query("SELECT post FROM Post post")
    @EntityGraph("Post.Resource")
    Page<Post> findAllPostResources(Pageable pageable);

    @Query("SELECT post FROM Post post")
    @EntityGraph("Post.Resource")
    Page<Post> findAllPostResources(Predicate predicate, Pageable pageable);
}

问题是当我使用谓词和可分页参数调用findAllPostResources时:

public Page<Post> getAllPostsByCountryPaginated(long countryId, Pageable pageable) {
    return postRepository.findAllPostResources(QPost.post.country.id.eq(countryId), pageable);
}

它忽略谓词参数,并在查询后执行:

SELECT
    post0_.id AS id1_13_0_,
    country1_.id AS id1_3_1_,
    post0_.body AS body2_13_0_,
    post0_.country_id AS country_7_13_0_,
    post0_.created_at AS created_3_13_0_,
    post0_.published_at AS publishe4_13_0_,
    post0_.title AS title5_13_0_,
    post0_.updated_at AS updated_6_13_0_,
    country1_.alpha2_code AS alpha2_3_1_,
    country1_.alpha3_code AS alpha3_3_1_,
    country1_.created_at AS created_4_3_1_,
    country1_.name AS name5_3_1_,
    country1_.updated_at AS updated_6_3_1_
FROM
    posts post0_
LEFT OUTER JOIN countries country1_ ON
    post0_.country_id = country1_.id
LIMIT ?

如您所见,SQL(WHERE country_id = ?)中没有WHERE原因。

那么,如何创建findAll()方法并定义谓词,分页以及JpaRepository内部使用的实体图?还是这种方式无法实现,我将需要创建自定义存储库实现?

2 个答案:

答案 0 :(得分:0)

但是您在查询中有此内容:

[[10, 11, 12], [13, 14, 15]]

与“ where country_id =”不一样。 您是否在示例数据库上运行了该查询,然后看到了什么?也许就可以了。

我想,你也可以说

LEFT OUTER JOIN countries country1_ ON
    post0_.country_id = country1_.id
LIMIT

抱歉,我想发表评论而不是回答,但是我没有足够的声誉。

答案 1 :(得分:0)

我为我的问题找到了解决方案:如何同时使用Predicate,Pageable和EntityGraph的所有方法,答案是使用以下库:

https://github.com/Cosium/spring-data-jpa-entity-graph

相关问题