I have abstract class AbstractCompound. Then I have classes DefaultCompound and RegistrationCompound which both extend A.
i want to perform a special type of search on all of these classes. For that I created a custom repository interface and an actual implementation for AbstractCompound:
And the actual repository:Code:public class AbstractCompoundRepositoryImpl<T extends AbstractCompound> implements MySpecialSearchRepository<T> { @PersistenceContext private EntityManager entityManager; @Autowired private CacheManager cacheManager; public AbstractCompoundRepositoryImpl() { } public AbstractCompoundRepositoryImpl(EntityManager entityManager, CacheManager cacheManager) { this.entityManager = entityManager; this.cacheManager = cacheManager; } @Override public Page<T> findByMySpecialDataType(...){ //.. complex method } }
1 service class needs this repository as it needs to query for any type of Compound. Other service classes must only query for a specific type of compound. hence I created repositories for the concrete classes:Code:@Repository public interface AbstractCompoundRepository<T extends AbstractCompound> extends MySpecialSearchRepository<T>, JpaRepository<T, Long>, QueryDslPredicateExecutor<T> { }
Code:@Repository public interface TestCompoundRepository extends AbstractCompoundRepository<TestCompound> { }Now when running tests from below class:Code:public class TestCompoundRepositoryImpl extends AbstractCompoundRepositoryImpl<TestCompound> { public TestCompoundRepositoryImpl() { super(); } public TestCompoundRepositoryImpl(EntityManager entityManager, CacheManager cacheManager) { super(entityManager, cacheManager); } }
I occurence extremly weird behavior:Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ApplicationContext.xml") public class CompoundRepositoryTest { @Autowired private TestCompoundRepository testCompoundRepository; @Autowired private RegistrationCompoundRepository registrationCompoundRepository; //... }
2 tests that use registrationCompoundRepository always pass
the other 3 tests use testCompoundRepository. From these tests 1 always passes, the other 2 always fail, however which one passes seems to be somewhat random (race condition probably) The failing test throw an exception:
Note: this object is inserted into database at start of test using import.sql (anything in there is executed by hibernate on start-up).Code:org.hibernate.WrongClassException: Object with id: 1000 was not of the specified subclass: myPackage.RegistrationCompound (loaded object was of wrong class class myPackage.TestCompound)
I'm now completely lost how I can solve the issue. It seems somewhere deep within spring it uses the wrong repository (the proxy) for whatever reason. Even more obscure is that 1 test using testCompoundRepository always passes, namely the first one run.
My question is: What is the issue? How can I solve it? Should I use a completely different approach?


Reply With Quote