I have following class structure:

I have a a spring-data repository (using QueryDSL) for AbstractCompound:
@Repository
public interface AbstractCompoundRepository<T extends AbstractCompound>
extends JpaRepository<T, Long>, QueryDslPredicateExecutor<T> {
}
and a Service class that calls that repository:
@Service
@Transactional
public class CompoundServiceImpl<T extends AbstractCompound>
implements CompoundService<T> {
@Autowired
private AbstractCompoundRepository<T> compoundRepository;
private Class<T> compoundClass;
private Class<? extends EntityPathBase<T>> compoundQueryClass;
private PathBuilder<T> pathBuilder;
@Autowired
public CompoundServiceImpl(Class<T> compoundClass,
Class<? extends EntityPathBase<T>> compoundQueryClass) {
this.compoundClass = compoundClass;
this.compoundQueryClass = compoundQueryClass;
String path = compoundClass.getSimpleName();
pathBuilder = new PathBuilder<>(compoundClass, path);
}
@Override
public T findOne(Predicate predicate) {
return compoundRepository.findOne(predicate);
}
//...snipped...
}
ApplicationContext.xml defines a service(bean) for each concrete class.
If I run tests, I get the mentioned exception:
WrongClassException - object with id was not of the specified subclass RegistrationCompound
I can verify that the correct service is called with the correct compoundClass. However the repository always seem to expect a RegistrationCompound.
I assume this is the cause due to autowiring the repository. I suspect only one instance is created and that instance expects a RegistrationCompound. My question is how can I have a type-specific repository for each service class? Is it even possible to have such a generic repository?
EDIT:
Now I’m starting to get pissed. What I want, my design, seems very basic to me. But with every such framework used fr anything just slightly different than in the examples it just breaks on all ends. I wonder how people actually use this for anything that isn’t completely simplistic.
I did refactor certain things as suggested by willome. However I still get the WrongClassException. The thing is there is also a the Class AbstractCompoundRepositoryImpl with custom behaviour. And all methods in that class throw that excpetion. it seem obvious that spring still only create 1 instance of this (always using RegistrationCompound as Type). The issue is this class contains the actually complex logic I’m trying to abstract away. If an separate implementation of this for each concrete class has to be made, it is unusable for my purpose. I don’t get it. I’m telling spring to create 2 different repositories so please do what I tell you spring? Ok? seriously…
EDIT 2:
And there is also some race condition. it is not always the same tests that throw this error, it varies most certainly depending on which Compound Type spring decides to use for creating the repository (instead of creating 2 like I’m telling it to do…)
I suspect too that the cause of your problem is the @autowired repository.
I would have change your code like this :