gpt4 book ai didi

java - JPA 查询 : java. lang.IllegalArgumentException:至少提供 1 个参数,但查询中仅存在 0 个参数

转载 作者:行者123 更新时间:2023-12-01 17:33:59 24 4
gpt4 key购买 nike

当我想通过userId查询最年轻(按日期)记录且userId不为空时,会抛出异常。这个方法写得正确还是有错误?

我有以下组件:

存储库

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

实体

@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;

嵌入 ID

@Embeddable
public class CustomEventDataId implements Serializable {

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

异常

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.(PartTreeJpaQuery.java:149) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.(PartTreeJpaQuery.java:270) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:79) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE] ... 70 common frames omitted

最佳答案

JPA 不期望查询中出现 userId,因为您使用 UserIdNotNull 这意味着查询中不期望 user_id IS NOT NULL 用户ID。使用这种方式userId

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

您不需要检查 not null,因为您正在传递 userId。

关于java - JPA 查询 : java. lang.IllegalArgumentException:至少提供 1 个参数,但查询中仅存在 0 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61075224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com