helloApp
helloApp

Reputation: 459

Java JPA: Unable to find all records between 2 LocalDates

Hello i am new to JPA and i have created an application where i want to find all the users posts that where made from the start of the month to the last day of the month.My model only contains the date the post has been created at as you can see below.I want to retrieve all the posts that where created in that specific month.I have a method below where i am using LocalDate(threetenbp dependency) to provide that(not sure if this is correct).

@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;
    }

As you can see in this method i am providing the current date, and the first day of the month and the last day of the month and then i call the repository with those values.

@Repository
public interface PostRepository extends CrudRepository<Post, Long> {
    List<Post> findAllBetweenCreatedAtandend(LocalDate start, LocalDate end);
}

Problem is the repository method is not working.I am getting errors like

(org.threeten.bp.LocalDate,org.threeten.bp.LocalDate)! No property No property findAllBetweenCreatedAtandend found for type Post!

Please help me create a working method for my repository that returns the posts between the 2 dates. Thank you !

Upvotes: 1

Views: 176

Answers (1)

crizzis
crizzis

Reputation: 10716

The correct method name would be findAllByCreatedAtBetween

Also, I'm not sure what 'LocalDate(threetenbp dependency)' is, but JPA only works with parameter types it understands how to map to database types. Adding support for custom types as query parameters is a pain in JPA, and I'm not even entirely sure it's possible with Spring Data on top.

the problem is that i won't have the methods provided here LocalDate start = now.withDayOfMonth(1); LocalDate end = now.withDayOfMonth(now.lengthOfMonth());

You can use TemporalAdjusters.firstDayOfMonth() and TemporalAdjusters.lastDayOfMonth()

Upvotes: 1

Related Questions