Hibernate 冬眠+;spring数据:ErrorClassException-id为的对象不属于指定的子类

Hibernate 冬眠+;spring数据:ErrorClassException-id为的对象不属于指定的子类,hibernate,inheritance,spring-data,Hibernate,Inheritance,Spring Data,我的班级结构如下: 我有一个用于AbstractCompound的spring数据存储库(使用QueryDSL): @Repository public interface AbstractCompoundRepository<T extends AbstractCompound> extends JpaRepository<T, Long>, QueryDslPredicateExecutor<T> { } 我可以验证是否使用正确的compoun

我的班级结构如下:

我有一个用于AbstractCompound的spring数据存储库(使用QueryDSL):

@Repository
public interface AbstractCompoundRepository<T extends AbstractCompound>
    extends JpaRepository<T, Long>, QueryDslPredicateExecutor<T> {
}
我可以验证是否使用正确的compoundClass调用了正确的服务。然而,存储库似乎总是期望注册化合物

我认为这是由于自动连接存储库造成的。我怀疑只创建了一个实例,该实例需要一个RegistrationCompound。我的问题是如何为每个服务类拥有一个特定于类型的存储库?甚至有可能拥有这样一个通用存储库吗

编辑:

现在我开始生气了。我想要的,我的设计,对我来说似乎很基本。但是,使用每一个这样的框架时,都会出现与示例中稍有不同的情况,这一切都会中断。我想知道人们是如何用它来做任何不是完全简单化的事情的

我确实按照willome的建议重构了某些东西。然而,我仍然得到了错误的类异常。问题是,类AbstractCompoundRepositoryImpl也有一个自定义行为。这个类中的所有方法都会抛出这个例外。很明显,spring仍然只创建了一个实例(总是使用RegistrationCompound作为类型)。问题是这个类包含了我试图抽象的复杂逻辑。如果必须为每个具体类单独实现此功能,那么就我而言,它是不可用的。我不明白。我告诉spring创建两个不同的存储库,所以请按照我告诉你的做?好啊说真的

编辑2:


还有一些比赛条件。抛出此错误的测试并不总是相同的,它的变化很大程度上取决于spring决定使用哪种复合类型来创建存储库(而不是像我告诉它的那样创建2个…)

我也怀疑问题的原因是@autowired存储库

我会像这样更改您的代码:

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;
   }
}
公共抽象类AbstractCompoundService
实现复合服务{
...
受保护的抽象AbstractCompoundRepository getCompoundRepository();
@凌驾
公共T findOne(谓词){
返回getCompoundRepository().findOne(谓词);
}
...
}
公共注册复合服务扩展了AbstractCompoundService{
@自动连线
注册复合存储库注册复合存储库;
受保护的AbstractCompoundRepository getCompoundRepository(){
返回registrationCompoundRepository;
}
}

是的,这正是我真正想要避免的,为每个具体类创建一个存储库和服务。是否可以不使用autowire并在配置中创建bean/存储库?当然,您可以删除@autowired注释并在配置文件中声明bean。但这是完全相同的。使用这个类模型,您必须为每个具体类声明一个存储库/服务。我看不到其他解决办法。否则,重构代码以实现您想要的…问题是如何在配置文件中声明存储库?它是一个接口,实际的类在运行时由spring数据生成。问题是整个设计更加复杂,至少有一个类需要一个应用于AbstractCompound的所有子类的CompoundRepository。使用Autowiring,spring现在可以找到3个匹配的类,因为它们扩展了AbstractCompoundRepository
WrongClassException - object with id was not of the specified subclass RegistrationCompound
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;
   }
}