查询方法 public abstract java.util.List 验证失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49817396/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:08:10  来源:igfitidea点击:

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

javaspring-mvcspring-bootspring-dataspring-data-jpa

提问by en Peris

I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. The version of SpringBoot is 2.0.1.RELEASE. I've created a class repository that extends from CrudRepository with this method

我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。SpringBoot 的版本是 2.0.1.RELEASE。我用这个方法创建了一个从 CrudRepository 扩展的类存储库

@Query("select us.priceAlertsTapas.tapa from User us left join us.priceAlertsTapas  pat left join pat.tapa tapa where pat.priceAlert = ?1")
    List<Tapa> tapasByUserPriceAlert (PriceAlert pa);

But when I init the project I got this error:

但是当我初始化项目时,我收到了这个错误:

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



    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76)
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:139)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:553)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery(RepositoryFactorySupport.java:546)
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1049)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:548)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new
 @Query("select us.priceAlertsTapas.tapa from User us ")
(RepositoryFactorySupport.java:538) at java.util.Optional.map(Optional.java:215) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:538) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:287) at org.springframework.data.util.Lazy.getNullable(Lazy.java:141) at org.springframework.data.util.Lazy.get(Lazy.java:63) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:102) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698) ... 47 common frames omitted Caused by: java.lang.NullPointerException: null at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:424) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3931) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3717) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3595) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) at com.sun.proxy.$Proxy105.createQuery(Unknown Source) at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) ... 76 common frames omitted

With the solution proposed I got the same error

使用提出的解决方案,我得到了同样的错误

With this query I have also the same error (?!?)

有了这个查询,我也有同样的错误(?!?)

@Entity
@Table(name="t_user")
public class User implements Serializable, UserDetails {

...

@ManyToMany
    @JoinTable(
        name="t_user_price_alert_tapa",
        joinColumns=@JoinColumn(name="user_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="price_alert_tapa_id", referencedColumnName="id"))
    private Set<PriceAlertTapa> priceAlertsTapas = new HashSet<>();
}

Here the User object:

这里是 User 对象:

@Entity
@Table(name="t_price_alert")
public class PriceAlert implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    public PriceAlert(int id) {
        super();
        this.id = id;
    }


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id; 

    private String name;

...

    }

and

@Entity
@Table(name="t_price_alert_tapa")
public class PriceAlertTapa  implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;


    public PriceAlertTapa(PriceAlert priceAlert, Tapa tapa) {
        super();
        this.tapa = tapa;
        this.priceAlert = priceAlert;
    }

    private Tapa tapa;

    private PriceAlert priceAlert;
..
}

and

@Query("select us.priceAlertsTapas.tapa from User us")

采纳答案by Andriy Slobodyanyk

Since priceAlertsTapas is Set you can't use dot-path.

由于 priceAlertsTapas 已设置,因此您不能使用点路径。

@Query("select pat.tapa from User us join us.priceAlertsTapas pat")

First you need to join it

首先你需要加入它

@Query("select tapa fom PriceAlertsTapas pat join pat.tapa tapa where pat.priceAlert = ?1")
List<Tapa> tapasByUserPriceAlert (PriceAlert pa);

回答by IonD

You have no reason to use a left (outer) join if your are only interested in the rightmost entity - although I'm not sure that this is the reason for which the validation of your query fails.

如果您只对最右边的实体感兴趣,则没有理由使用左(外部)连接 - 尽管我不确定这是查询验证失败的原因。

I would try this:

我会试试这个:

@Transactional
@Modifying
@Query("from SHIPPING_DOC e where e.fulfillmentId in ?1")
List<ShippingDocumentsJsonEntity> deleteByFulfillmentIdIn(List<String> fulfillmentIds);

If a given "tapa" can have multiple association to a "price alert" (eg one per user) then you will find it multiple times in your return List. The solution is either to change the return type to Set<Tapa>or to use "select distinct...."in your query.

如果给定的“tapa”可以与“价格警报”有多个关联(例如每个用户一个),那么您会在退货中多次找到它List。解决方案是将返回类型更改为Set<Tapa>"select distinct...."在查询中使用。

If, on the other hand, your PriceAlertsTapas is a simple many-to-many association, it might be better to use the @ManyToMany JPA annotation, eventually with @JoinTable. See, for example, here.

另一方面,如果您的 PriceAlertsTapas 是一个简单的多对多关联,则最好使用 @ManyToMany JPA 注释,最终使用 @JoinTable。例如,请参见此处

回答by Vitalii Muzalevskyi

You have this exception because you have wrong select:

你有这个例外,因为你有错误的选择:

usis User

usUser

us.priceAlertsTapasis Set

us.priceAlertsTapasSet

after that you truing to get tapafrom it but Setdon't have such field.

在那之后,你真的想从中获得tapa,但Set没有这样的领域。

回答by avinash kumar

I was getting this error as I was using table name in @Query(), but you have to use class name in @Query():

我在 中使用表名时遇到此错误@Query(),但您必须在 中使用类名@Query()

Incorrect:

不正确:

@Transactional
@Modifying
@Query("from ShippingDocumentsJsonEntity e where e.fulfillmentId in ?1")
List<ShippingDocumentsJsonEntity> deleteByFulfillmentIdIn(List<String> fulfillmentIds);

Correct:

正确的:

##代码##