Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateAdd function not support in jpa query

DateAdd function is working fine as a sql query in sql-server as i want to substract some number of days from a date and i am getting result but while same i am using in jpa project, spring boot project has not started.

Below the repository class, if i comment out this below line of code, spring boot project starts as expected.

public interface domainRepository extends CrudRepository<domainTable , Long> {

    @Query("Select DATEADD(day,-(1), d.date) from  domainTable d "
           + "where d.id in (:id)")
    public Date getDate(@Param("id") Long id); 

}

How to fix this? or i do have to write a separate function instead of DATEADD?

like image 475
Kaustav Avatar asked Nov 29 '25 15:11

Kaustav


1 Answers

Actually, JPA doesn't support time periods operations because not all databases support it. So you have following options:

1- Calculate date programmatically (Java side, using calendar API or Java 8 Date Time API).

2- Use native query.

like image 186
Nour Eddine Avatar answered Dec 02 '25 03:12

Nour Eddine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!