java 查询之间的春季数据日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45754413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring data date between query
提问by Ayan Pal
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:
我可以将上述查询转换为 spring 数据 jpa 查询方法吗?例如:
findByProductId(productId)
or I only have to use @Queryor a Named queryto 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.
或者我只需要使用@Query或命名查询来处理此类查询。在提出问题之前,我尝试先在这里搜索以及 spring 数据站点,但没有找到任何解决方案。任何帮助表示赞赏。
回答by Roel Strolenberg
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 String
s.
请注意,您必须输入date
两次才能匹配两个“where”子句。这是假设您使用的是Date
对象,而不是文字String
s。
See JPA RepositoriesTable 2.3 for more info.
有关更多信息,请参阅JPA 存储库表 2.3。
回答by Cepr0
You can try something like this trick:
你可以试试这个技巧:
select
p
from
Product p
where
(?1 = 'field1' and p.field1 between p.effctDate and p.expDate)
or (?1 = 'field2' and p.field2 between p.effctDate and p.expDate)
or (?1 = 'field3' and p.field3 between p.effctDate and p.expDate)
or (?1 = 'field4' and p.field4 between p.effctDate and p.expDate)
...
But this is not the best approach IMO...
但这不是 IMO 的最佳方法......
To build dynamic queries you can use thees ones:
要构建动态查询,您可以使用这些查询:
回答by Vaibhav Gupta
You can try to use something like below :
您可以尝试使用以下内容:
List findAllByDateApprovedBetween(String dateApprovedFrom, String dateApprovedTo);
列出 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 ?)
休眠:选择 applicatio0_.id 作为 id1_1_,applicatio0_.date_approved 作为 date_app4_1_ from application applicatio0_ where (applicatio0_.date_approved between ? and ?)