spring-data-jpa Spring jpa存储库按属性查找行,并仅返回最新的行,

ki1q1bka  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(184)

我有实体:

@Entity
@Table(name = "forum_comment", schema = "public")
public class ForumCommentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne
    @JoinColumn(name = "post", nullable = false)
    private ForumPostEntity post;

    @Column(name = "created", columnDefinition = "TIMESTAMP")
    private LocalDateTime created;
}

我想找到属于论坛帖子的最新论坛评论。我有JpaRepository

public interface ForumCommentRepository extends JpaRepository<ForumCommentEntity, Long> {
    long countByPost(ForumPostEntity entity);
    LocalDateTime findAllByPostAndFindFirstByOrderByCreatedDesc(ForumPostEntity entity);
}

然而,它抱怨不知道FindFirst等等。声明函数的正确方法是什么?首先按属性过滤,然后对它们进行排序,只返回最新的行。
感谢您的帮助!

nnt7mjpx

nnt7mjpx1#

您必须在开头移除findAll。它必须是:
findFirstByPostOrderByCreatedDesc

相关问题