0

I have a repository B which I extend from another repository A.

Repository A:

@Repository
public class RepositoryA implements Repository {

}

Repository B:

@Repository
public class RepositoryB extends  RepositoryA implements RepositoryAux{
}

But when I try to inject the repository I am having the following error:

No unique bean of type [Repository] is defined: expected single matching bean but found 2: [RepositoryA , RepositoryB]

I understand that I can solve it using the annotation @Qualifier. But, my question is:

  1. it's right Overwrite the annotation @Repository in the child class ?

  2. Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name

Thank's in advance

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Matías W.
  • 350
  • 2
  • 17
  • Your question is unclear. --- 1) What do you means by "overwrite" `@Repository`? The `@Qualifier` annotation must be given *in addition to* the `@Repository` annotation, not instead of. --- 2) `@Qualifier` has nothing to do with record/row identifier (field/column). – Andreas Jul 04 '17 at 02:09
  • Possible duplicate of [Best way of handling entities inheritance in Spring Data JPA](https://stackoverflow.com/questions/27543771/best-way-of-handling-entities-inheritance-in-spring-data-jpa) – xyz Jul 04 '17 at 04:50
  • FWIW XXXRepository is **nothing** to do with the JPA API, and everything to do with Spring Data JPA. Corrected tagging – Neil Stockton Jul 04 '17 at 05:57

1 Answers1

3
  1. it's right Overwrite the annotation @Repository in the child class ?

There are a few things you should consider:

a. If you intend to use RepositoryA as a repository, hen you need @Repository on both.

b. If you intent to use RepositoryA just to provide basic functionality so RepositoryB (or other classes) can extend it, then make RepositoryA abstract and annotate with NoRepositoryBean

c. Alternatively, you could use "composition over inheritance" principle. In short you could inject RepositoryA into RepositoryB and create facade or wrapper methods to access the methods on RepositoryB instance.

  1. Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name

I think you mean @Named from javax.inject.Named which is basically equivalent to @Qualifier

I think you mean <bean id='...' which in this case @Qualifier is the way to do with annotations. See spring's documentation

Rafa
  • 1,997
  • 3
  • 21
  • 33