2017-01-31 17 views
0

私は、次の構成の簡単なエンティティの継承ツリーを持っている:Spring Data Jpaはリポジトリの継承とその処理方法をサポートしていますか?

各クラスは、独自のリポジトリがあります:ItemRepositoryChartRepositoryTableRepositoryを。 3つのリポジトリはすべてです。そのため、ドメインルールの

ここで説明したように、私は、削除にカスタムロジックを実装する必要があります。http://docs.spring.io/spring-data/jpa/docs/1.10.3.RELEASE/reference/html/#repositories.custom-implementations

をこのカスタム削除ロジックの懸念は、継承ツリー内のすべてのエンティティ(抽象1および2つのコンクリート)。

ItemRepositoryにカスタムロジックを実装し、子エンティティのリポジトリを拡張する方法はありますかItemRepository

これにより、継承ツリーの各エンティティに同じロジックが重複することを回避できます。ただ、削除...

EDIT

上で実行されるコードの1 LIGNEを実装するために= 9 classes

EntityRepository + EntityRepositoryCustom + EntityRepositoryImpl X 3 entities:それがなければ、これは定型クラスの多くを作りますユーザーuser7398104の答えは、herethereのようなものを実装する方法を私に教えてくれましたが、これらのリソースは@NoRepositoryBeanベースリポジトリにカスタムリポジトリを実装する方法を説明していません。私は運なしで次のことを試してみました:私はコメントに提案されているようItemBaseRepositoryCustomに@NoRepositoryBeanを設定しようとしましたが、それはどちらかの仕事をdoesntの

@NoRepositoryBean 
public interface ItemBaseRepository<T extends Item> extends 
    CrudRepository<T, Integer>, 
    ItemBaseRepositoryCustom<T> 

@RepositoryRestResource 
public interface ItemRepository extends ItemBaseRepository<Item> {} 

@RepositoryRestResource 
public interface ChartRepository extends ItemBaseRepository<Item> {} 

@RepositoryRestResource 
public interface TableRepository extends ItemBaseRepository<Item> {} 

public interface ItemBaseRepositoryCustom<T extends Item> { 
    public void delete (T i); 
} 

public class ItemBaseRepositoryImpl<T extends Item> 
    implements ItemBaseRepositoryCustom<T> { 

    public void delete (T i) { 
     // Custom logic 
    } 

} 

編集編集

を。

+0

Spring Data Commons Ref:[Springデータリポジトリのカスタム実装](http://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.custom-implementations) – Cepr0

+0

こんにちは、ありがとう、しかし、これは私の質問に投稿された同じリンクです。私はカスタムリポジトリの実装を作成する方法を知っていますが、私は継承に固執しています。 –

+0

申し訳ありません@tiben - 気が散らない! ( – Cepr0

答えて

0

ItemRepositoryCustomインターフェイスを作成して、ItemRepositoryインターフェイスを拡張し、各チャートとテーブルがItemRepositoryCustomレポを拡張するようにすることができます。このインタフェースでは、削除操作のためのカスタム実装を行うことができます。

EDIT:これはうまくいきません。

しかし、次のアプローチが有効です。

interface CustomItemRepository<T extends Item> extends CrudRepository<T, Long>{ }

interface ChartRepository extends CustomItemRepository<Chart>{ }

interface TableRepository extends CustomItemRepository<Table>{ }

次に、あなたはCustomItemRepositoryのために削除ロジックのためのクラスを作成することができます。

+0

私はこのソリューションを試しましたが、残念ながらそれはうまくいきませんでした。私は 'ViewRepositoryCustom'インターフェース、ViewRepositoryImpl'クラスを試してみました。テストのために' ChartRepository'インターフェース'ViewRepositoryCustom'と' ChartRepositoryImpl'の名前を変更するだけで、 'ViewRepositoryCustom'と' ViewRepositoryImpl'を専門に扱っていましたので、抽象エンティティクラスではうまく動作しません。 –

+0

こんにちは。私はカスタムリポジトリを実装することができません。質問に私の編集を参照してください。 –

+0

@NoRepositoryBeanこれはItemBaseRepositoryCustomリポジトリ上にある必要があります。これはカスタムリポジトリであり、Springは個別にプロキシクラスを作成すべきではありません – userJ

関連する問題