Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data date between query

I have a query like below:

select * from Products p where ? between  p.effctDate and p.expDate

Can I translate the above query to a spring data jpa query method? Like for example:

findByProductId(productId)

or I only have to use @Query or a Named query to handle such types of queries. I tried searching here first before asking the question as well as spring data site but did not find any solution. any help is appreciated.

like image 828
Ayan Pal Avatar asked Aug 18 '17 10:08

Ayan Pal


Video Answer


2 Answers

You could use the following query:

Date date = //Input date
List<Product> = findByEffctDateAfterAndExpDateBefore(date, date);

Note that you have to enter date twice to match both 'where' clauses. This is under the assumption you are using Date objects, not literal Strings.

See JPA Repositories Table 2.3 for more info.

like image 186
Roel Strolenberg Avatar answered Oct 21 '22 07:10

Roel Strolenberg


You can try to use something like below :

List findAllByDateApprovedBetween(String dateApprovedFrom, String dateApprovedTo);

It translates to :

Hibernate: select applicatio0_.id as id1_1_, applicatio0_.date_approved as date_app4_1_ from application applicatio0_ where (applicatio0_.date_approved between ? and ?)

like image 45
Vaibhav Gupta Avatar answered Oct 21 '22 06:10

Vaibhav Gupta