带有 CURRENT_DATE - 1 的 JPQL 查询不起作用
JPQL query with CURRENT_DATE - 1 does not work
我是 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[=13=](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[=13=](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
JPA 定义 CURRENT_DATE
/ CURRENT_TIME
/ CURRENT_TIMESTAMP
是引用当前日期/时间的可移植方式。
此外,一些实现(不是全部!)支持函数,如 year
/ month
/ day
/ hour
/ minute
/ second
.
在您的代码中,您使用 days
。您可以将其替换为 day
。不过是提取月份的日期,不知道是不是你想要的。
如果你想引用 'yesterday',你可以尝试使用 CURRENT_DATE-1
但它不是必需的可移植的(一些数据库可以,其他的则不行)。
你可以试试:
@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);
}
我是 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[=13=](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[=13=](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
JPA 定义 CURRENT_DATE
/ CURRENT_TIME
/ CURRENT_TIMESTAMP
是引用当前日期/时间的可移植方式。
此外,一些实现(不是全部!)支持函数,如 year
/ month
/ day
/ hour
/ minute
/ second
.
在您的代码中,您使用 days
。您可以将其替换为 day
。不过是提取月份的日期,不知道是不是你想要的。
如果你想引用 'yesterday',你可以尝试使用 CURRENT_DATE-1
但它不是必需的可移植的(一些数据库可以,其他的则不行)。
你可以试试:
@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);
}