Spring 数据 JPA 无法为方法创建查询
Spring Data JPA Failed to create query for method
我想列出这样的订单
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 引导和 Spring 数据 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”中错了。谢谢!
对不起我的英语不好。这是我第一次 post 我的 question.xD
尝试像这样编辑方法:
List<Post> findAllByOrderByCreatedDateDesc();
因为看起来 table 列名称和实例名称不同,因此您必须如下所示注释实例变量:
@Column(name="created_date")
您的方法名称似乎有误。
findByOrderBycreateDateDesc()
应该是 findByOrderByCreatedDateDesc()
,属性 名称以大写字母开头。
我想列出这样的订单
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 引导和 Spring 数据 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”中错了。谢谢! 对不起我的英语不好。这是我第一次 post 我的 question.xD
尝试像这样编辑方法:
List<Post> findAllByOrderByCreatedDateDesc();
因为看起来 table 列名称和实例名称不同,因此您必须如下所示注释实例变量:
@Column(name="created_date")
您的方法名称似乎有误。
findByOrderBycreateDateDesc()
应该是 findByOrderByCreatedDateDesc()
,属性 名称以大写字母开头。