Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crudrepository How to find by last date between?

My domain has and Date field updated, that I want to search by

@Column(name = "updated")
Date updated;

I have a Java Date object representing a day, that's passed in by my endpoint's controller.

Date day;

and a crudrepository representing my data

public interface DataRepository extends CrudRepository<Data, Long> {

   List<Data> findByLastUpdatedInDate(Date date);

}

Obviously the above method doesn't work, but is there something similar? Or am I going to have to do a find between then manually search for the last entry?

Edit: Here's how I get day; dateString is passed in by the controller.

SimpleDateFormat dateFormatIn = new SimpleDateFormat("yyyy-MM-dd");
Date day = dateFormatIn.parse(dateString);
like image 946
Damien Avatar asked Dec 02 '25 10:12

Damien


2 Answers

You're almost there. The slight tweak would be this if I read the documentation correctly:

Data data = dataRepository.findTopByUpdatedBetweenOrderByUpdatedDesc(start, stop);

Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result

like image 169
Jonck van der Kogel Avatar answered Dec 05 '25 00:12

Jonck van der Kogel


You can annotatw your method with @Query annotation and write HQL query

@Query("select d from Data d where d.updated= :date")
List<Data> findByLastUpdatedInDate(@Param("date") Date date);
like image 26
Peter1982 Avatar answered Dec 05 '25 00:12

Peter1982