Skip to content
Advertisement

How to execute a native SQL query having INTERVAL clause through Spring Boot JPA?

Not able to execute a native SQL query through Spring Boot Repository layer with JPA in Oracle DB

I am trying to write a native SQL query that gets data from a table between the current date and last 30 days.

The query that works fine in TOAD or SQL Developer is:

But when I try to run it through a Spring Boot application that has a repository layer it gives me following error message.

MoneyController.java

MoneyRepository.java

Error:

Advertisement

Answer

It’s almost certainly easier through JPA and other API’s that interact with databases to use the numToDSInterval function rather than the interval literal syntax

That way, you’re just doing a very normal operation of passing a numeric parameter to a function rather than hoping that the database persistence layer understands the interval literal syntax.

You might be able to do simple date arithmetic

Be aware, however, that this will implicitly cast systimestamp as a date so you’ll use the fractional seconds of precision. That may not be a problem for you– I doubt that you care about the difference between something that happened 3 days and 4 milliseconds ago vs. 3 days and 0 milliseconds– but it is something to be aware of. If there is a reason that you are using systimestamp rather than sysdate, you probably want to keep your data types as timestamps.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement