3

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);
Damien
  • 263
  • 1
  • 4
  • 15
  • 1
    Its not clear what you are asking? what is the date that is passed to controller? do you want to search based on that ? can you explain a bit better – pvpkiran Jun 22 '18 at 21:01
  • I archive data from an external source, then provide it through a restful service that takes a date in the format yyyy-MM-dd; I convert that to a Java date object, and that's day. – Damien Jun 22 '18 at 21:05
  • can you give an example – pvpkiran Jun 22 '18 at 21:07
  • SimpleDateFormat simpleForm = new SimpleDateFormat("yyyy-MM-dd"); Date day = simpleForm.parse(dateString); – Damien Jun 22 '18 at 21:08
  • you want to search by this date in database? your topic headline says last date between. what is between? between which dates? – pvpkiran Jun 22 '18 at 21:10
  • I probably named the question poorly; the only solution to a similar problem I found, was to use find between to search between the start and end of the provided day, rather than using the day in the query. Even that leaves me manually finding the last one. – Damien Jun 22 '18 at 21:12
  • take a look at [this](https://stackoverflow.com/questions/39784344/check-date-between-two-other-dates-spring-data-jpa) the method name may need to be findByUpdated...no field lastUpdated... then you can use `findByUpdatedBetween(Date start,Date end)` – mavriksc Jun 22 '18 at 21:17
  • findByUpdatedBetween works for giving me all the entries in that day, but I need to get the last one; I can do that with a custom query, or by searching through the list find between gives me, but I'd rather use something from the framework if it exists. OrderBy looks promising, I might let it give me the list and just take the top element. – Damien Jun 22 '18 at 21:20
  • FYI: `java.util.Date` is a terrible class that was replaced years ago by `java.time.Instant`. Search Stack Overflow to learn more about the *java.time* classes. – Basil Bourque Jun 22 '18 at 22:51

3 Answers3

5

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

Jonck van der Kogel
  • 2,983
  • 3
  • 23
  • 30
1

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);
Peter1982
  • 518
  • 2
  • 11
  • probably not interested in the date being an exact match. but on or after a certain day and before another – mavriksc Jun 22 '18 at 21:19
  • Am I missing something, this looks like it'd work the same as List findByUpdated(Date date); – Damien Jun 22 '18 at 21:24
  • Yes, this is same as findByUpdated(Date date). In comment above you mention that you have working custom query. Could you posted for showing what you want achieve. You can used order by and Pageable as part of request – Peter1982 Jun 22 '18 at 21:35
  • I said I could make one, I haven't yet though; the solution I'm using right now is; data = dataRepository.findByUpdatedBetweenOrderByUpdatedDesc(start, stop).get(0); Where Start is 22 June 2018 00:00:00 and Stop is 23 June 2018 00:00:00, I start with 22 june 2018 and need to get the last entry for that day. The method I just mentioned works, but I'd like something a bit more elegant. – Damien Jun 22 '18 at 21:45
  • And what about List findByUpdatedBetweenByUpdatedDesc(Date start, Date end, Pageable pageable); and call will look like this.findByUpdatedBetweenByUpdatedDesc(new Date(), new Date(), PageRequest.of(0, 1)) – Peter1982 Jun 22 '18 at 21:49
  • You could also try Data findFirstByUpdatedBetweenByUpdatedDesc(Date start, Date end), you can also return Optional – Peter1982 Jun 22 '18 at 21:52
  • I just added an answer to my question with what I'm using right now, but I'm not going to mark it as the answer; I was hoping for something more elegant. Your PageRequest looks like it'll do what I want, but it doesn't look any different than what I did with a list and get. Are pages less costly than lists? – Damien Jun 22 '18 at 21:54
  • in you answer is error becase for empty result you get Exception and Data findFirstByUpdatedBetweenByUpdatedDesc(Date start, Date end) doesnt work for you? – Peter1982 Jun 22 '18 at 21:56
  • I didn't know First was a thing, but yeah findFirstByUpdatedBetweenOrderByUpdatedDesc(start,stop); works the same as findByUpdatedBetweenOrderByUpdatedDesc(start,stop).get(0); – Damien Jun 22 '18 at 22:04
0

I'm hoping someone finds something more elegant, but for now what I'm using is

List<Data> findByUpdatedBetweenOrderByUpdatedDesc(Date start, Date stop);

And converting day with

Date start = new Date(day.getTime());
Date stop = new Date(day.getTime() + 86400000L);

to get my entry with

Data data = dataRepository.findByUpdatedBetweenOrderByUpdatedDesc(start, stop).get(0);
Damien
  • 263
  • 1
  • 4
  • 15