简体   繁体   中英

Spring Data JPA date query doesn't work for Date been and date greater than

I'm trying to query using Spring data JPA, with the following queries. But I could get the fileted output between any given dates.

@Query(value = "SELECT new com.xxx.DonationByUserTotal(d.club.name, d.club.id , d.club.logo, SUM(d.amount)) " +
        "from Donation d where d.user.id=?1 and d.createdDate  >= ?2 GROUP BY d.club")
List<DonationByUserTotal> findByUserAndFromDate(int User, @Param("fromDate") Date fromDate);

This query returns all the dates for the given user but doesn't filter between given dates.

@Query("SELECT new com.xxx.DonationByUserTotal(d.club.name, d.club.id , d.club.logo, SUM(d.amount) )" +
        "from Donation d where d.user.id=?1 and d.createdDate BETWEEN ?2 and  ?3  GROUP BY d.club")
List<DonationByUserTotal> findByUserAndBetweenDates(int User,@Param("from")Date fromDate,@Param("to") Date toDate);

This query returns an empty set despite valid dates between.

Can someone help me to solve this?

PS:

The entity class is using the

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false, updatable = false)
private Date createdDate;

and the parameters passed to query also with a timestamp.

I was able to figure, that the error was due to a different format in date. Because the spring didn't throw any errors, it was difficult to figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM