JPA query: java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query

huangapple go评论104阅读模式
英文:

JPA query: java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query

问题

以下是您提供的内容的翻译:

异常在我尝试使用非空userId查询最年轻(按日期)的记录时抛出。这个方法写得正确吗,还是有一些错误?

我有以下组件:

Repository

public interface CustomEventStorage extends JpaRepository<CustomEventData, CustomEventDataId> {
    CustomEventData findFirstByUserIdNotNullOrderByIdDateDesc(@Param("userId") String userId);
}

Entity

@Entity
@Table(name = "event")
public class CustomEventData {

    @EmbeddedId
    private CustomEventDataId id;

    @Column(name = "state")
    @Enumerated(EnumType.STRING)
    private EventState state;
    @Column(name = "user_id")
    private String userId;
}

Embeddable id

@Embeddable
public class CustomEventDataId implements Serializable {

    @Column(name = "event_id")
    private String eventId;
    @Column(name = "date")
    private LocalDateTime date;
}

Exception

Caused by: java.lang.IllegalArgumentException: At least 1 parameter(s)
provided but only 0 parameter(s) present in query.
    at org.springframework.util.Assert.isTrue(Assert.java:136) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.data.jpa.repository.query.QueryParameterSetterFactory$CriteriaQueryParameterSetterFactory.create(QueryParameterSetterFactory.java:291)
    at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createQueryParameterSetter$1(ParameterBinderFactory.java:139)
    ...

(此处省略了部分异常堆栈信息)

请注意,上述翻译仅为您提供的内容的直译。如果您需要更多解释或帮助,请随时询问。

英文:

The exception is thrown when I want to query for the youngest (by date) record by userId with userId not empty.. Is this method written correct or there is some error ?

I have following components:

Repository

public interface CustomEventStorage extends JpaRepository&lt;CustomEventData, CustomEventDataId&gt; {
    CustomEventData findFirstByUserIdNotNullOrderByIdDateDesc(@Param(&quot;userId&quot;) String userId);
}

Entity

@Entity
@Table(name = &quot;event&quot;)
public class CustomEventData {

    @EmbeddedId
    private CustomEventDataId id;

    @Column(name = &quot;state&quot;)
    @Enumerated(EnumType.STRING)
    private EventState state;
    @Column(name = &quot;user_id&quot;)
    private String userId;

Embedabble id

@Embeddable
public class CustomEventDataId implements Serializable {

    @Column(name = &quot;event_id&quot;)
    private String eventId;
    @Column(name = &quot;date&quot;)
    private LocalDateTime date;

Exception

> Caused by: java.lang.IllegalArgumentException: At least 1 parameter(s)
> provided but only 0 parameter(s) present in query.
> at org.springframework.util.Assert.isTrue(Assert.java:136) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
> at org.springframework.data.jpa.repository.query.QueryParameterSetterFactory$CriteriaQueryParameterSetterFactory.create(QueryParameterSetterFactory.java:291)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createQueryParameterSetter$1(ParameterBinderFactory.java:139)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
> ~[na:1.8.0_242]
> at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
> ~[na:1.8.0_242]
> at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
> ~[na:1.8.0_242]
> at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
> ~[na:1.8.0_242]
> at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:531)
> ~[na:1.8.0_242]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createQueryParameterSetter(ParameterBinderFactory.java:141)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createSetters$0(ParameterBinderFactory.java:131)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
> ~[na:1.8.0_242]
> at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
> ~[na:1.8.0_242]
> at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
> ~[na:1.8.0_242]
> at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
> ~[na:1.8.0_242]
> at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
> ~[na:1.8.0_242]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createSetters(ParameterBinderFactory.java:132)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createSetters(ParameterBinderFactory.java:124)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createCriteriaBinder(ParameterBinderFactory.java:75)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.getBinder(PartTreeJpaQuery.java:250)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:149)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:270)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:79)
> ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
> ... 70 common frames omitted

答案1

得分: 0

JPA没有预期查询中的 userId,因为您使用了 UserIdNotNull,这意味着查询中的 user_id IS NOT NULL,而不需要期望 userId。请使用这种方式 userId

CustomEventData findFirstByUserIdOrderByIdDateDesc(@Param("userId") String userId);

您不需要检查非空,因为您正在传递 userId。

英文:

JPA didn't expect userId in query because you use UserIdNotNull which means user_id IS NOT NULL in query which don't expect userId. Use this way userId

CustomEventData findFirstByUserIdOrderByIdDateDesc(@Param(&quot;userId&quot;) String userId);

You don't need to check not null becasuse you are passing userId.

huangapple
  • 本文由 发表于 2020年4月7日 15:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61075224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定