Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:58:49+00:00 2026-06-18T09:58:49+00:00

I have following class structure: I have a a spring-data repository (using QueryDSL) for

  • 0

I have following class structure:

Class Diagramm

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…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:58:50+00:00Added an answer on June 18, 2026 at 9:58 am

    I suspect too that the cause of your problem is the @autowired repository.

    I would have change your code like this :

    public abstract class AbstractCompoundService<T extends AbstractCompound>
    implements CompoundService<T> {
      ...
      protected abstract AbstractCompoundRepository<T> getCompoundRepository();
    
      @Override
      public T findOne(Predicate predicate) {
        return getCompoundRepository().findOne(predicate);
      }
      ...
    }
    public RegistrationCompoundService extends AbstractCompoundService<RegistrationCompound> {
    
       @Autowired
       RegistrationCompoundRepository registrationCompoundRepository;
    
       protected AbstractCompoundRepository<RegistrationCompound> getCompoundRepository(){
          return registrationCompoundRepository;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following data structure (simplified): A giant list of the class TestClass public
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I have the following data structure: public class Person { public string Name{get;set;} public
I have the following class structure public class Outer{ private Mapper a; .... private
I have the following class structure: public class DBTestItem { public int p0; public
I have a collection of objects which has the following structure: public class Parent
I have data that has been stored using binary serialization for the following class:
I have the following data-structure as a class named Task: private: string name; int
I have the following data structure: //property Notification abstract class BindableBase { } //base
I have the following directory structure. src| |pack| |Test.java data| |data.txt classes| |pack| |Test.class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.