0

I have the following mysql query that I want to convert to SPring JPA.

SELECT * FROM SERVER s WHERE s.COMPLETE_DT BETWEEN (DATE_SUB('2014-11-07 16:30:20', INTERVAL 20 MINUTE)) 
AND (DATE_SUB('2014-11-07 16:30:20', INTERVAL 10 MINUTE)) 

I am doing it the following way

@Query("SELECT s FROM Server s WHERE s.completeDt BETWEEN (DATE_SUB(%?1%, INTERVAL 20 MINUTE)) AND (DATE_SUB(%?1%, INTERVAL 10 MINUTE)))
List<Server> findDate(Date dt);

but getting an error

"Interval" not recognized

1
  • 1
    There are many jpql docs on the web. None will have date_sub, or interval. Commented Oct 19, 2017 at 15:57

1 Answer 1

2

With Spring Data you could use a query like :

List<Server> findByCompleteDtBetween(final LocalDateTime startDate, final LocalDateTime endDate);

And compute startDate and endDate in your service to fit the targeted interval.

See more : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.