通过Repository JPA中的Query获取自定义对象列表

时间:2017-03-01 18:20:02

标签: spring spring-data spring-data-jpa jpql

我有问题tu构建JPA查询。

我想有一份EmployeeTimesheetDTO列表

public class EmployeeTimesheetDTO {
    private String name;
    private String employeeId;
    private String filename;
}

由函数参数给出的时间段。但是,重要的是员工列表必须始终显示员工不存在的事件时间表(列表中的空值)

@Query("SELECT new package.domain.EmployeeTimesheetDTO( em.employeeID as employeeId, em.name as name, tse.filename as filename)" +
        " FROM EmployeeEntity em LEFT JOIN TimesheetEntity tse ON em.employeeID=tse.id.ownerId WHERE tse.id.period =:period")
List<EmployeeTimesheetDTO> findAllEmployeeTimesheet(@Param("period") String period);

我收到了错误:

Validation failed for query for method public abstract java.util.List

我尝试运行程序时出现错误;

aused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wi
th name 'codaFacadeImpl': Unsatisfied dependency expressed through field 'employeeService'; nested
exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'employeeServiceImpl': Unsatisfied dependency expressed through field 'repository'; neste
d exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with na
me 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArg
umentException: Validation failed for query for method public abstract java.util.List package.c
oda.persistance.repository.EmployeeRepository.findAllEmployeeTimesheet(java.lang.String)!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$Autowi
redFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.6.RELEASE.
jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.
java:88) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postPr
ocessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar
!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBea
n(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBea
n(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(
AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFa
ctory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Defa
ultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFact
ory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactor
y.java:202) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(Dependenc
yDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency
(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(D
efaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$Autowi
redFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.
jar!/:4.3.6.RELEASE]
        ... 27 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wi
th name 'employeeServiceImpl': Unsatisfied dependency expressed through field 'repository'; nested
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name
 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgum
entException: Validation failed for query for method public abstract java.util.List package.cod
a.persistance.repository.EmployeeRepository.findAllEmployeeTimesheet(java.lang.String)!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$Autowi
redFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.6.RELEASE.
jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.
java:88) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postPr
ocessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar
!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBea
n(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBea
n(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(
AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFa
ctory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Defa
ultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFact
ory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactor
y.java:202) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(Dependenc
yDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency
(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(D
efaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$Autowi
redFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.
jar!/:4.3.6.RELEASE]
        ... 40 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '
employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumen
tException: Validation failed for query for method public abstract java.util.List package.coda.
persistance.repository.EmployeeRepository.findAllEmployeeTimesheet(java.lang.String)!
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeB
ean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBea
n(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(
AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFa
ctory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Defa
ultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFact
ory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactor
y.java:202) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(Dependenc
yDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency
(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(D
efaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$Autowi
redFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.
jar!/:4.3.6.RELEASE]
        ... 53 common frames omitted
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstra
ct java.util.List package.coda.persistance.repository.EmployeeRepository.findAllEmployeeTimeshe
et(java.lang.String)!
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuer
y.java:92) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:
62) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(
JpaQueryFactory.java:72) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQue
ryFactory.java:53) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookup
Strategy.resolveQuery(JpaQueryLookupStrategy.java:144) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQue
ryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:212) ~[spring-data-jpa-1.11.0.RELEASE.jar
!/:na]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookup
Strategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorM
ethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.0.RELEASE.jar
!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(
RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.0.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndRet
urn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.0.RELEASE.jar!/:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPrope
rtiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.0.RELEASE.jar!/:na]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterProperties
Set(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitM
ethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEA
SE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeB
ean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]

        ... 63 common frames omitted
Caused by: java.lang.NullPointerException: null
        at org.hibernate.hql.internal.ast.HqlSqlWalker.setAlias(HqlSqlWalker.java:1097) ~[hibernate
-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.aliasedSelectExpr(HqlSqlBaseWalker.jav
a:2514) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2653
) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2355)
 ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2
225) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:149
6) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:581) ~[hib
ernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:
309) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:257) ~
[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
 ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:19
0) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
 ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-co
re-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) ~[hibernate-cor
e-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~
[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
 ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[h
ibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) ~[hibernate-core-5
.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.ja
va:291) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_112]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_112]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_112]
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocation
Handler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEAS
E]
        at com.sun.proxy.$Proxy83.createQuery(Unknown Source) ~[na:na]
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuer
y.java:86) ~[spring-data-jpa-1.11.0.RELEASE.jar!/:na]
        ... 76 common frames omitted

1 个答案:

答案 0 :(得分:1)

WHERE替换为AND,如果TimesheetEntity中出现WHERE条件,那么它就像INNER JOIN一样:

@Query("SELECT new package.domain.EmployeeTimesheetDTO( em.employeeID as employeeId, em.name as name, tse.filename as filename)" +
        " FROM EmployeeEntity em LEFT JOIN TimesheetEntity tse ON em.employeeID=tse.id.ownerId AND tse.id.period =:period")
List<EmployeeTimesheetDTO> findAllEmployeeTimesheet(@Param("period") String period);