我是JPA2.0
和Spring Data JPA
的新手,我尝试在其中获取详细信息where createdDt > Sysdate and createdDt > sysdate-1
。我不确定是否可以使用CURRENT_DATE-1函数来实现sysdate-1,但无法正常工作。
以下选项的任何帮助都无法正常工作吗?我也尝试过YESTERDAY
,但这也失败了。
此处,我不希望使用PersistanceContext和实体管理器查询进行开发。
@Query("SELECT e FROM CustMstr e WHERE e.crteDt < CURRENT_DATE AND (DAYS(e.crteDt) > DAYS(CURRENT_DATE) - 1) AND e.accActive ='Y'")
List<CustMstr> findByEmailAndAcc();
另一种方式
@Query("SELECT e FROM CustMstr e WHERE e.crteDt < CURRENT_DATE AND (DAYS(e.crteDt) > CURRENT_DATE - 1) AND e.accActive ='Y'")
List<CustMstr> findByEmailAndAcc();
下面的错误
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exampleClient': Unsatisfied dependency expressed through field 'CustMstrRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustMstrRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.XX.XX.repository.CustMstrRepository.findByEmailAndAcc()!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:367)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.XX.XX.app.ExampleMain.main(ExampleMain.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustMstrRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.XX.XX.repository.CustMstrRepository.findByEmailAndAcc()!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:578)
... 15 more
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT e FROM CustMstr e WHERE e.crteDt < CURRENT_DATE AND (DAYS(e.crteDt) > DAYS(CURRENT_DATE) - 1) AND e.acc_active ='Y'].
[62, 103] The right expression is not a valid expression.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1616)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
at com.sun.proxy.$Proxy22.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)
... 54 more
答案 0 :(得分:0)
JPA将CURRENT_DATE
/ CURRENT_TIME
/ CURRENT_TIMESTAMP
定义为引用当前日期/时间的可移植方式。
此外,某些实现(并非全部!)支持功能,例如year
/ month
/ day
/ hour
/ minute
/ second
。
在您的代码中,您使用days
。您可以将其替换为day
。但这是提取月份中的某天,不确定是您想要的。
如果要引用“昨天”,则可以尝试使用CURRENT_DATE-1
,但这不是可移植的(某些DB可以接受,而其他则不行)。
您可以尝试:
@Query("SELECT e FROM CustMstr e WHERE e.crteDt between CURRENT_DATE-1 AND CURRENT_DATE AND e.accActive ='Y'")
List<CustMstr> findByEmailAndAcc();
但是也许您需要在Java端进行管理:
在存储库方面:
@Query("SELECT e FROM CustMstr e WHERE e.crteDt between :start and :end AND e.accActive ='Y'")
List<CustMstr> findByEmailAndAcc(Date start, Date end);
在服务端:
List<CustMstr> findByEmailAndAcc(){
Date start=new Date();
Date end=new Date(date.getTime()-(24*3600));
return repository.findByEmailAndAcc(start, end);
}