我有两个 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 中使用什么实体图?或者这是无法通过这种方式实现的,我需要创建自定义存储库实现?