2017-09-22 6 views
0

<T> T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory)をすべてのリポジトリに紹介します。 新しいインターフェイスが作成されました春のデータリポジトリを拡張する

@NoRepositoryBean 
public interface ExtendedJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 
    T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory); 
} 

public class ExtendedJpaRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedJpaRepository<T, ID> { 

    private final JpaEntityInformation entityInformation; 
    private final EntityManager entityManager; 

    public ExtendedJpaRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { 
     super(entityInformation, entityManager); 
     this.entityInformation = entityInformation; 
     this.entityManager = entityManager; 
    } 

    @Override 
    public T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory) { 
     throw new NotImplementedException("No implemented yet"); 
    } 
} 

次に、このインタフェースを具体的なリポジトリに使用します。 RecipeIngredientRepository:

public interface RecipeIngredientRepository extends ExtendedJpaRepository<RecipeIngredient, Long> {} 

私はようやく私のサービスにリポジトリを注入すると、私は次の例外を取得:

java.lang.IllegalStateException: Failed to load ApplicationContext 
... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recipeIngredientRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property find found for type RecipeIngredient! Did you mean 'id'? 

それは私のentitiy RecipeIngredientfindプロパティを探しています。私はこれをやりたいとは思わなかった。これはJPA Query Methodsに関連していると思います。だから、名前をfindOrCreateからxxxに変更して、成功していないクエリメソッドの検出をバイパスしました。次にxxxプロパティを検索します。

春データがこのプロパティを検索するのは何ですか? 私はorg.springframework.boot:spring-boot-starter-data-jpaを使用しています。

答えて

0

@EnableJpaRepositories(repositoryBaseClass = ExtendedJpaRepositoryImpl.class)でカスタマイズしたリポジトリの実装を指定する必要があります。

リファレンスドキュメント:Adding custom behavior to all repositoriesをご覧ください。

+0

ありがとうございました!まあ、私はこの文書に従った!しかし、@EnableJpaRepositories( "org.project.data.repository")を使って、repositoryBaseClassの代わりにbasePackagesを指定しました。 @EnableJpaRepositoriesアノテーションでエラーが発生するとは思っていませんでした。 – kfaria

関連する問題