Johnyb
Johnyb

Reputation: 1316

Spring jpa repository find rows by property and return only the latest one

I have entity:

@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;
}

And i want to find the latest ForumComment that belongs to the ForumPost. I have JpaRepository:

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

However it is complaining about not knowing FindFirst and so on What is the correct way to declare function that first filter by property and then order them and returns only the latest row?

Thanks for help!

Upvotes: 1

Views: 817

Answers (1)

Leandro Luque
Leandro Luque

Reputation: 538

You have to remove the findAll at the beginning. It must be:

findFirstByPostOrderByCreatedDesc

Upvotes: 2

Related Questions