Spring Data JPA 无法为方法创建查询

发布于 2025-01-11 11:51:54 字数 2054 浏览 0 评论 0原文

我想列出这样的订单

select * from post
Order by created_date desc

这是

//Mapper
public class Post {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long postId;
    @NotBlank
    private String postName;
    @Nullable
    private String url;
    @Nullable
    @Lob
    private String description;
    private Integer voteCount = 0;
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private User user;
    @Column(name="created_date")
    private Instant createdDate;
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id")
    private Sub sub;
    public Integer getVoteCount() {
        if (this.voteCount == null) {
            return 0;
        }
        return this.voteCount;
    }
    

}
//Repository
public interface PostRepository extends JpaRepository<Post, Long> {
    List<Post> findByOrderBycreateDateDesc();
    List<Post> findAllByDescriptionContaining(String description);
    List<Post> findAllByPostNameContaining(String postName);
    List<Post> findAllBySub(Sub sub);
    List<Post> findByUser(User user);
    
}

//Service

    @Transactional(readOnly = true)
    public List<PostResponse> getAllPosts() {
        return postRepository.findByOrderBycreateDateDesc()
                .stream()
                .map(postMapper::mapToDto)
                .collect(toList());
    }

我正在使用 Java Spring Boot 和 Spring Data JPA 的 代码 我遇到了这样的错误,

 Could not create query for public abstract java.util.List com.Website.Step2.repository.PostRepository.findAllOrderbycreateDateDesc()!
 Reason: Failed to create query for method public abstract java.util.List com.Website.Step2.repository.PostRepository.findAllOrderbycreateDateDesc()! 
 No property findAllOrderbycreateDateDesc found for type Post!

你们可以帮助我吗!我认为我在“findByOrderBycreateDateDesc”中错了。谢谢! 对不起我的英语。这是我第一次发表我的问题。xD

I want to list an order like

select * from post
Order by created_date desc

And here is my code

//Mapper
public class Post {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long postId;
    @NotBlank
    private String postName;
    @Nullable
    private String url;
    @Nullable
    @Lob
    private String description;
    private Integer voteCount = 0;
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private User user;
    @Column(name="created_date")
    private Instant createdDate;
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id")
    private Sub sub;
    public Integer getVoteCount() {
        if (this.voteCount == null) {
            return 0;
        }
        return this.voteCount;
    }
    

}
//Repository
public interface PostRepository extends JpaRepository<Post, Long> {
    List<Post> findByOrderBycreateDateDesc();
    List<Post> findAllByDescriptionContaining(String description);
    List<Post> findAllByPostNameContaining(String postName);
    List<Post> findAllBySub(Sub sub);
    List<Post> findByUser(User user);
    
}

//Service

    @Transactional(readOnly = true)
    public List<PostResponse> getAllPosts() {
        return postRepository.findByOrderBycreateDateDesc()
                .stream()
                .map(postMapper::mapToDto)
                .collect(toList());
    }

I'm using Java Spring Boot and Spring Data JPA
I got some error like this

 Could not create query for public abstract java.util.List com.Website.Step2.repository.PostRepository.findAllOrderbycreateDateDesc()!
 Reason: Failed to create query for method public abstract java.util.List com.Website.Step2.repository.PostRepository.findAllOrderbycreateDateDesc()! 
 No property findAllOrderbycreateDateDesc found for type Post!

Can u guys help me! I think i was wrong in "findByOrderBycreateDateDesc". Thanks!
Sorry for my english. This is the first time i post my question.xD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

负佳期 2025-01-18 11:51:54

尝试像这样编辑方法:

List<Post> findAllByOrderByCreatedDateDesc();

因为它看起来表列名称和实例名称不同,因此您必须注释实例变量,如下所示:

@Column(name="created_date")

Try to edit the method like this:

List<Post> findAllByOrderByCreatedDateDesc();

As it looks the table column name and the instance name are different therefore you have to annotate the instance variable as show below:

@Column(name="created_date")
站稳脚跟 2025-01-18 11:51:54

您的方法名称似乎已关闭。
findByOrderBycreateDateDesc() 应为 findByOrderByCreatedDateDesc(),且属性名称以大写字母开头。

Your method name seems to be off.
findByOrderBycreateDateDesc() should be findByOrderByCreatedDateDesc() with the property name starting with a capital letter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文