如何正确实施 Spring Data JPA 存储库方法来检索与对象列表相关的所有对象? (不是一个)

How can I correctly implement a Spring Data JPA repository method retrieving all the objects related to a list of object? (not to a single one)

我正在使用 Spring Data JPA 开发 Spring 启动应用程序,我遇到了以下问题。

我有这个 EventLog 实体 class:

@Entity
@Table(name = "log")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class EventLog implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "source_user_fk", referencedColumnName = "id")
    //@JsonManagedReference(value = "sourceUser")
    private User sourceUser;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "destination_user_fk", referencedColumnName = "id")
    private User destinationUser;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "source_wallet_fk", referencedColumnName = "id")
    private Wallet sourceWallet;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "destination_wallet_fk", referencedColumnName = "id")
    private Wallet destinationWallet;

    @Column(name = "notes")
    private String notes;


    @Column(name = "timestamp")
    private LocalDateTime timestamp;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "log_type_fk", referencedColumnName = "id")
    //@Column(name = "log_type_fk")
    private LogType logType;
}

如您所见,它包含此 LogType 字段:

@ManyToOne
@EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
@JoinColumn(name = "log_type_fk", referencedColumnName = "id")
private LogType logType;

然后我创建了以下存储库界面:

public interface LogRepository extends JpaRepository<EventLog, Integer>, JpaSpecificationExecutor {

    EventLog findById(String id);

    Page<EventLog> findByLogTypeAndTimestampBetween(LogType logType, LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);

    // HERE THE PROBLEM !!!
    Page<EventLog> findByLogTypesAndTimestampBetween(List<LogType> logTypesList, LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);

    
    Page<EventLog> findByTimestampBetween(LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);

}

在这个存储库中,我有这个 findByLogTypeAndTimestampBetween() 方法检索 Page 指定单个 LogType 对象作为查询输入参数(基本上它 return 所有与特定类型日志相关的日志)。效果很好。

然后我尝试创建以下 findByLogTypesAndTimestampBetween() 方法,该方法采用 LogType 的 lkist 并且必须 return与列表中所有日志类型相关的日志。

问题是添加第二种方法,运行 我的应用程序在堆栈跟踪中收到以下错误消息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logRepository' defined in com.easydefi.users.repository.LogRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! Reason: Failed to create query for method public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! No property logTypes found for type EventLog! Did you mean 'logType'?; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! No property logTypes found for type EventLog! Did you mean 'logType'?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.12.jar:5.3.12]
    ... 101 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! Reason: Failed to create query for method public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! No property logTypes found for type EventLog! Did you mean 'logType'?; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! No property logTypes found for type EventLog! Did you mean 'logType'?
    at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
    at java.base/java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1056) ~[na:na]
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:96) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new[=14=](QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:360) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.12.jar:5.3.12]
    ... 112 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.easydefi.users.repository.LogRepository.findByLogTypesAndTimestampBetween(java.util.List,java.time.LocalDateTime,java.time.LocalDateTime,org.springframework.data.domain.Pageable)! No property logTypes found for type EventLog! Did you mean 'logType'?
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.5.6.jar:2.5.6]
    ... 134 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property logTypes found for type EventLog! Did you mean 'logType'?
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.mapping.PropertyPath.lambda$from[=14=](PropertyPath.java:366) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new[=14=](PartTree.java:249) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:250) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new[=14=](PartTree.java:383) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:384) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:95) ~[spring-data-commons-2.5.6.jar:2.5.6]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    ... 138 common frames omitted

怎么了?我错过了什么?如何正确定义存储库方法,将与所有对象相关的所有 EventLog 对象检索到 List logTypesList 参数中?

Spring JPA 正在寻找 属性 'logTypes',如错误所示。

您想在提供的列表中找到LogType

  Page<EventLog> findByLogTypeInAndTimestampBetween(List<LogType> logTypesList, LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);

参考:
Supported keywords inside method names

阅读日志:

No property logTypes found for type EventLog! Did you mean 'logType'?

并查看您字段的变量:

private LogType logType;

改变

Page<EventLog> findByLogTypesAndTimestampBetween(...);

Page<EventLog> findByLogTypeAndTimestampBetween(...);