M
M
maestro072020-04-24 17:34:11
Java
maestro07, 2020-04-24 17:34:11

How to get data by date in jpa repository?

I need to get data by date. For example, all data for 2020-02-19.

Wrote a request

List<Client> findByUserIdAndRegistrationDate(UUID id, Date date);


request: ..../a42cea80-480a-11ea-a82f-00155d101a20/2020-02-19

5ea2f6cb062f3790272973.png

there is data in the database. I specifically send such a request because I know that there is data.

Maybe due to the fact that I send 2020-02-19 and in the database 2020-02-19 23:36:00 and they are not equal

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Simankin, 2020-04-25
@VladimirNSimankin

1. To search by date for all users (and in the question I did not see that a selection was needed for one user), UUID is not needed.
2. Yes

Maybe due to the fact that I send 2020-02-19 and in the database 2020-02-19 23:36:00 and they are not equal
so it is, the date in this case is supplemented with 00:00:00, that is, the search occurs at the beginning of the day.
3. As azerphoenix wrote above, you need to use findAll to get the Client list.
4. You need to use
findAllByRegistrationDateBetween(дата_начала_периода, дата_окончания_периода)

5. You can add time to the start and end dates of the period. By the start date of the period 00:00:00 to the end date of the period 23:59:59 for correct sampling. If you do not add time, then you need to arrive one day by the end date of the period, the selection as a whole will be correct.
6. If you still need a selection for a specific user, then we use the same thing, but add userId:
findAllByUserIdAndRegistrationDateBetween(user_id, дата_начала_периода, дата_окончания_периода)

O
Orkhan, 2020-04-24
Hasanly @azerphoenix

List<Client> findByUserIdAndRegistrationDate(UUID id, Date date);

Firstly,
findAllBy if you want to get a list, instead of findBy
Secondly, can you show exactly how you store the date in the entity? LocalDate, LocalDateTime, Date, etc.

R
RabyAliaksandr, 2020-04-25
@RabyAliaksandr

use Date from java.sql.Date and don't bother

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question