12

I have a simple Spring Data JPA project working with mysql and I need to fetch all registers that match the day and month. The column I need to filter is type Datetime.

1935-12-08 00:00:00

If I want to do this in a db level it works fine:

SELECT * FROM my_database.event where event_date LIKE '%-12-08%';

It treats the date as a string. Now I need to do this in my repository yet nothing seems to work. I tried the basic one:

List<Event> findByEventDateLike(
        @Param("eventDate") Date eventDate);

but it says it returns an illegal argument exception since it is a Date object. I tried other combinations but apparently spring data jpa can't compare dates with partial information.

NOTE: To keep it clean I'm trying to avoid @Query sentences but if it is the only way to go, it is a valid answer.

How can I do it?

Matias Diez
  • 1,237
  • 2
  • 17
  • 26
  • If you want to use like '%%' you have to chage the type to String – marti_ Dec 08 '17 at 18:08
  • How about change method name to `findByEventDateGreaterThanAndEventDateLessThan(Date startDate, Date endDate)` – Zeronex Dec 08 '17 at 19:04
  • you can find more details here https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html – Zeronex Dec 08 '17 at 19:05
  • Yes the problem with that in particular is that I need it to return something like "On this day in history" so It should return everything that happened on this day through the years. – Matias Diez Dec 08 '17 at 21:04
  • @Osgux is there a way to do it without modifying my eventDate property in my Entity? I'm using it as it is in other calls. – Matias Diez Dec 08 '17 at 21:07
  • could be create a native query and parse date to string like 2017-12-08 – marti_ Dec 08 '17 at 22:10

4 Answers4

16

If your JPA provider is Hibernate then you can use year and month (and other funcs) in your JPQL (HQL) query, for example like this:

@Query("select e from Event e where year(e.eventDate) = ?1 and month(e.eventDate) = ?2")
List<Entity> getByYearAndMonth(int year, int month);
Cepr0
  • 28,144
  • 8
  • 75
  • 101
7

Use nativeQuery to emulate the query in the Database.

@Query(value = "SELECT * FROM events WHERE event_date Like %?1%", nativeQuery = true)
List<Match> findByMatchMonthAndMatchDay(@Param ("eventDate") String eventDate);

The DB takes care of the conversion between Date/String for the LIKE clause.

Passing the param as "-month-day" (e.g.: -12-08) will return a collection of events with that string in the date field.

Matias Diez
  • 1,237
  • 2
  • 17
  • 26
3

The code below works for ZonedDateTime (startDate), don't have an opportunity to test against DateTime right now:

@Query("SELECT b FROM Box b " +
        "WHERE EXTRACT (day FROM b.startDate) = :dayOfMonth AND EXTRACT (month FROM b.startDate) = :month")
List<Box> findBoxWithParticularDayAndMonth(@Param("dayOfMonth") Integer dayOfMonth, @Param("month") Integer month);

Usage:

List<Box> boxes = boxRepo.findBoxWithParticularDayAndMonth(22, 5);
dimirsen Z
  • 873
  • 13
  • 16
1

Java: Date to String Convert java.util.Date to String

@Query("select e from Event e where e.eventDate like %:eventDate%")
List<Entity> findByEventDateLike(@Param("eventDate")String eventDate);

for me the easy way is parse Date to String but I found another solution you could be try

%Like% Query in spring JpaRepository https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

marti_
  • 130
  • 5
  • 14