gpt4 book ai didi

spring-boot - 您如何在 Spring Boot 应用程序中的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?

转载 作者:行者123 更新时间:2023-12-04 02:45:31 25 4
gpt4 key购买 nike

我试图在同一个域对象上同时使用 Spring Data JPA 和 Spring Data Elasticsearch,但它不起作用。

当我尝试运行一个简单的测试时,出现以下异常:

org.springframework.data.mapping.PropertyReferenceException: No property index found for type Person! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.Part.(Part.java:76) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:84) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:61) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:408) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) ~[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.9.0.RELEASE.jar:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] ... 43 common frames omitted



它们在禁用任何一个时起作用。

该项目基于 Spring Boot 1.3.0.M5。

这是一个重现这种情况的示例项目:

https://github.com/izeye/spring-boot-throwaway-branches/tree/data-jpa-and-elasticsearch

最佳答案

Spring Data 中的存储库与数据源无关,这意味着 JpaRepositoryElasticsearchRepository两者都汇总为 Repository界面。在这种情况下,Spring Boot 的自动配置将导致 Spring Data JPA 尝试为项目中继承任何 Spring Data Commons 基础存储库的每个存储库配置一个 bean。

要解决此问题,您需要将 JPA 存储库和 Elasticsearch 存储库移动到单独的包中,并确保注释您的 @SpringBootApplication应用程序类:

  • @EnableJpaRepositories
  • @EnableElasticsearchRepositories

  • 然后,您需要为每个启用注释指定存储库的位置。这最终看起来像:
    @SpringBootApplication
    @EnableJpaRepositories("com.izeye.throwaway.data")
    @EnableElasticsearchRepositories("com.izeye.throwaway.indexing")
    public class Application {

    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

    }

    然后您的应用程序将能够消除哪些存储库用于哪个 Spring Data 项目。

    关于spring-boot - 您如何在 Spring Boot 应用程序中的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32878813/

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