如何解决Java JPA:无法找到2个LocalDate之间的所有记录
您好,我是JPA的新手,我创建了一个应用程序,我想在其中查找从月初到该月最后一天所做的所有用户帖子。我的模型仅包含该帖子的日期我想检索在该特定月份创建的所有帖子。下面有一个方法,我正在使用LocalDate(threetenbp依赖项)提供该信息(不确定这是否正确)。
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private LocalDate createdAt;
private LocalDate modifiedAt;
@NotBlank(message = "Post Title is required")
private String title;
@Basic(fetch = FetchType.LAZY)
private String image;
@NotBlank(message = "Post Content is required")
private String content;
@OneToMany(cascade = CascadeType.ALL)
private List<Comment> comments = new ArrayList<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "POST_TAG",joinColumns = {@JoinColumn(name = "post_id")},inverseJoinColumns = {@JoinColumn(name = "tag_id")})
private Set<Tag> tags = new HashSet<>();```
public List<Post> doSomething() {
// something that should execute on 1st day every month @ 00:00
LocalDate now = LocalDate.now();
LocalDate start = now.withDayOfMonth(1);
LocalDate end = now.withDayOfMonth(now.lengthOfMonth());
List<Post> postsThisMonth = postRepository.findAllBetweenCreatedAtandend(start,end);
return postsThisMonth;
}
如您所见,在此方法中,我提供了当前日期,月份的第一天和月份的最后一天,然后使用这些值调用存储库。
@Repository
public interface PostRepository extends CrudRepository<Post,Long> {
List<Post> findAllBetweenCreatedAtandend(LocalDate start,LocalDate end);
}
问题是存储库方法不起作用。出现类似错误
(org.threeten.bp.LocalDate,org.threeten.bp.LocalDate)! No property No property findAllBetweenCreatedAtandend found for type Post!
请帮助我为我的存储库创建一个工作方法,该方法可以返回两个日期之间的帖子。 谢谢!
解决方法
正确的方法名称应为findAllByCreatedAtBetween
此外,我不确定“ LocalDate(threetenbp依赖项)”是什么,但是JPA仅适用于可理解如何映射到数据库类型的参数类型。在JPA中,添加对自定义类型的支持作为查询参数是JPA的一个难题,我什至不能完全确定Spring Data位于顶部。
问题是我将没有
这里提供的方法LocalDate start = now.withDayOfMonth(1); LocalDate end = now.withDayOfMonth(now.lengthOfMonth());
您可以使用TemporalAdjusters.firstDayOfMonth()
and TemporalAdjusters.lastDayOfMonth()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。