Announcement Announcement Module
Collapse
No announcement yet.
subclassing JPA repository -> WrongClass exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • subclassing JPA repository -> WrongClass exception

    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:
    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
    		}
        }
    And the actual repository:
    Code:
    	@Repository
    	public interface AbstractCompoundRepository<T extends AbstractCompound>
    			extends MySpecialSearchRepository<T>, JpaRepository<T, Long>,
    			QueryDslPredicateExecutor<T> {
    	}
    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 TestCompoundRepository extends AbstractCompoundRepository<TestCompound> {
    	}
    Code:
    	public class TestCompoundRepositoryImpl extends AbstractCompoundRepositoryImpl<TestCompound> {
    		public TestCompoundRepositoryImpl() {
    			super();
    		}
    
    		public TestCompoundRepositoryImpl(EntityManager entityManager,
    				CacheManager cacheManager) {
    			super(entityManager, cacheManager);
    		}
    	}
    Now when running tests from below class:
    Code:
    	@RunWith(SpringJUnit4ClassRunner.class)
    	@ContextConfiguration("classpath:ApplicationContext.xml")
    	public class CompoundRepositoryTest {
    
    		@Autowired
    		private TestCompoundRepository testCompoundRepository;
    		@Autowired
    		private RegistrationCompoundRepository registrationCompoundRepository;
    		
    		//...
    	}
    I occurence extremly weird behavior:

    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:
    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)
    Note: this object is inserted into database at start of test using import.sql (anything in there is executed by hibernate on start-up).

    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?
Working...
X