Spring Data JPA 无法为方法创建查询
我想列出这样的订单
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试像这样编辑方法:
因为它看起来表列名称和实例名称不同,因此您必须注释实例变量,如下所示:
Try to edit the method like this:
As it looks the table column name and the instance name are different therefore you have to annotate the instance variable as show below:
您的方法名称似乎已关闭。
findByOrderBycreateDateDesc()
应为findByOrderByCreatedDateDesc()
,且属性名称以大写字母开头。Your method name seems to be off.
findByOrderBycreateDateDesc()
should befindByOrderByCreatedDateDesc()
with the property name starting with a capital letter.