简体   繁体   中英

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!

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

findFirstByPostOrderByCreatedDesc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM