Inheritance management with Spring and JPA

0

Good morning

I need to create a repository "B" from a repository "A" which should no longer be modified. I have some doubts about how to proceed since I am having an ambiguity error on my first attempt.

I have my repository A:

@Repository
public class consultasRepositorioImpl implements consultasRepositorio
{
}

Now, in repository B I did the following:

@Repository
public class consultasRepositorioAuxImpl extends consultasRepositorioImpl   implements consultasRepositorioAux
{
}

The new operations that I am going to handle are in the interface RepositorioAux .

I want Spring to recognize my class RequestsAuxImpl as a new repository, but I am having the following error when trying to raise my application:

  

org.springframework.beans.factory.NoSuchBeanDefinitionException: No   unique bean of type [mx.repositories.consultasRepositorio] is defined:   expected single matching bean but found 2: [queriesRepositorioImpl,   queriesAuxImplRepository]

I understand that I can solve the above using the annotation @Qualifier where inject the bean. My question is, is there no other way to solve this problem of ambiguity? maybe I'm misapplying the inheritance or the annotations.

any support is appreciated

Greetings

    
asked by Matías W. 04.07.2017 в 18:39
source

1 answer

0

@Repository points to Spring's component-scan that this class is a component of your application, so it will check the interface it has checked at the moment of creating the bean. That is to say that it looks for a class that implements the interface that it has marked. Since he has 2 he does not know what to do. Effectively with @Qualifier solves the issue because it explicitly indicates which class should be used.

Although on the other hand, you can try doing Repository queriesImpl an abstract class without marking by @Repository and the one that extends if you mark it with the annotation. I do not know how it would apply in your case, but a case that I have seen is a scheme where there is a common management of ABM operations (a generic dao for example) and then the different particularities of the different model objects are served with a repository that that extends the generic with the differences or not that this requires. (Obviously, there are readings for and against this way)

public interface GenericDao<E, I extends Serializable> {

    void save(E entity);
...
}


public abstract class GenericDaoImpl<E, I extends Serializable> implements GenericDao<E, I>

    protected GenericDAOImpl(SessionFactory sessionFactory, Class<E> entityClass) {
        this.sessionFactory = sessionFactory;
            this.entityClass = entityClass;
        }

        public void save(E entity) {
            //logica de guardado
        }
}

@Component
public class AlgunDaoImpl extends GenericDAOImpl<Entidad, Clave> implements AlgunDao {

@Autowired
protected AlgunDaoImpl (SessionFactory sessionFactory) {
    super(sessionFactory, Entidad.class);
}
...
}

Since this is just an example, and more parts would be missing or have things like generic elements to serve different entities.

    
answered by 04.07.2017 в 22:55