简体   繁体   中英

Spring boot JPA repository query

I want to retrieve data of people who where born within 6 months from current date using spring jpa repository

+-----------+----------+-------------+-----------------+------------+-------------+
| donorName | donorAge | donorGender | donorBloodgroup | donorPhone | dateOfBirth |
+-----------+----------+-------------+-----------------+------------+-------------+
| abd       |       22 | male        | bpos            |       1212 | 1998-09-01  |
| Arun      |       22 | male        | apos            |      12111 | 1998-10-27  |
| rohit     |       21 | male        | apos            |    1233212 | 1999-09-01  |
| virat     |       32 | male        | apos            |   10001100 | 1999-09-01  |
| Rama      |       21 | male        | o-ve            |   12345678 | 2019-09-08  |
| ramesh    |       34 | male        | b+ve            |  123456712 | 2020-12-10  |
| Aniruddha |       22 | male        | o+ve            |  959121844 | 2020-08-01  |
+-----------+----------+-------------+-----------------+------------+-------------+

this is my table

and this is the query I'm trying: @Query("from donor where dateOfBirth > date_sub(now(),interval 6 month)") List findBloodDonorsBydateOfBirth(); Im getting errors while building the spring boot application. This query works in mysql Thanks

Two tips:

  1. use this query @Query( value = "select * from donor as d where d.dateofBirth > date_sub(now(),interval 6 month)", nativeQuery = true) List<Donor> findBloodDonorsBydateOfBirth();

  2. To give you a better help, please attach the error that comes out on the console.

Try below code in your Repository, it will work.

@Query("select d from donor d where d.dateOfBirth > date_sub(now(),interval 6 month)") 
List<Donor> findBloodDonorsBydateOfBirth();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM