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
CodePudding user response:
Your method name seems to be off.
findByOrderBycreateDateDesc()
should be findByOrderByCreateDateDesc()
with the property name starting with a capital letter.
CodePudding user response:
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")