您如何在 Spring Boot 应用程序的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库? [英] How do you use both Spring Data JPA and Spring Data Elasticsearch repositories on the same domain class in a Spring Boot application?

查看:24
本文介绍了您如何在 Spring Boot 应用程序的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use both Spring Data JPA and Spring Data Elasticsearch on the same domain object but it doesn't work.

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

When I tried to run a simple test, I got the following exception:

org.springframework.data.mapping.PropertyReferenceException:否找到了 Person 类型的属性索引!在org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.query.parser.Part.(Part.java:76)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.query.parser.PartTree.(PartTree.java:84)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:61)~[spring-data-jpa-1.9.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95)~[spring-data-jpa-1.9.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206)~[spring-data-jpa-1.9.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73)~[spring-data-jpa-1.9.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:408)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)~[spring-data-commons-1.11.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)~[spring-data-jpa-1.9.0.RELEASE.jar:na] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] ... 43个常用框架省略

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

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

They work when disabling either one.

项目基于 Spring Boot 1.3.0.M5.

The project is based on Spring Boot 1.3.0.M5.

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

This is a sample project reproducing the situation:

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

推荐答案

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

Repositories in Spring Data are datasource agnostic, meaning that JpaRepository and ElasticsearchRepository both roll up into Repository interface. When this is the case, then auto-configuration of Spring Boot will cause Spring Data JPA to try and configure a bean for each repository in the project that inherits any Spring Data Commons base repository.

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

To fix this problem you need to move your JPA repository and Elasticsearch repository to separate packages and make sure to annotate your @SpringBootApplication application class with:

  • @EnableJpaRepositories
  • @EnableElasticsearchRepositories

然后您需要为每个启用注释指定存储库的位置.这最终看起来像:

Then you need to specify where the repositories are for each enable annotation. This ends up looking like:

@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 项目.

Then your application will be able to disambiguate which repositories are intended for which Spring Data project.

这篇关于您如何在 Spring Boot 应用程序的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆