8

私は、同じドメインオブジェクト上でSpring Data JPAとSpring Data Elasticsearchの両方を使用しようとしていますが、動作しません。Spring Data JPAとSpring Data Elasticsearchリポジトリの両方を、Springブートアプリケーションの同じドメインクラスにどのように使用しますか?

org.springframework.data.mapping.PropertyReferenceException::Person型が見つかりません プロパティのインデックス、私は簡単なテストを実行しようとした

は、私は次の例外を持って! org.springframework.data.mapping.PropertyPath。(PropertyPath.java:75) 〜[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath。 (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] at org.springframework。 data.repository。 (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:235) 373) 〜[spring-data-commons-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree $述語(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] at org.springframework.data.jpa.repository.query.PartTreeJpaQue (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) 〜[春-データ-JPA-1.9.0.RELEASE.jar:ナ] org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206) で〜[春-データ-JPA-1.9.0.RELEASE.jar:ナ] org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73) 〜[春-データで-jpa-1.9.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。(Rep ositoryFactorySupport.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) 〜[春豆-4.2.1.RELEASE.jar:4.2.1.RELEASE] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.javaで:1574) 〜[春豆-4.2.1.RELEASE.jar:4.2.1.RELEASE] ... 43の共通フレーム は、いずれかを無効にするとき、彼らは仕事

を省略しました。

このプロジェクトは、Spring Boot 1.3.0.M5に基づいています。

これは、状況を再現するサンプルプロジェクトです:春データで

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

答えて

14

リポジトリはJpaRepositoryElasticsearchRepositoryは両方Repositoryインターフェースにロールアップすることを意味し、とらわれないデータソースです。この場合、SpringBootの自動設定により、Spring Data JPAは、Spring Data Commonsベースリポジトリを継承するプロジェクト内のリポジトリごとにBeanを設定します。その後

  • @EnableJpaRepositories
  • @EnableElasticsearchRepositories

:あなたは、パッケージを分離して、あなたの@SpringBootApplicationアプリケーションクラスに注釈を付けることを確認するためにJPAリポジトリとElasticsearchリポジトリを移動する必要がある。この問題を解決するには

リポジトリが各イネーブル注釈のどこにあるかを指定する必要があります。これは、次のようになります。

@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データプロジェクトに使用されているかを明確にすることができます。

+1

感謝。それは私が選んだ最終的な解決策へのヒントを与えました。私にとって、 'includeFilters'を使うことは、それらを別のパッケージに移動するよりも優れています。 Spring Dataのデータソースに依存しない性質は非常にいいですが、Spring Data JPAが可能であれば、 'ElasticsearchRepository'インターフェースをスキャンしなかったことはいいことでしょう。 –

+2

私がしたことに興味のある人は、https://github.com/izeye/spring-boot-throwaway-branches/commit/874ccba09189d6ef897bc430c43b6e3705404399 –

+0

を参照してくださいありがとうございますkenny-bastaniとjohnny-lim、私はあなたのために投票しました –

4

あなたはこのように使用することができます:

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class)) 
@EnableElasticsearchRepositories(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class)) 
public class DataConfiguration { 
    ... 
} 

またはSpringBoot中:詳細は

@SpringBootApplication 
@EnableJpaRepositories(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class)) 
@EnableElasticsearchRepositories(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class)) 
public class MyApplication { 
    ... 
} 
+0

'@ EnableJpaRepositories'に入っていれば' ElasticsearchCrudRepository.class'ではなく 'JpaRepository.class'を使うべきでしょうか? – edwin

+0

私は '@ EnableJpaRepositories'にexludeを使用しています。 Jpaリポジトリでは、 'JpaRepository.class'だけでなく、 –

+0

でもOKKをいただきました。 – edwin

関連する問題