【问题标题】:Is there anyway to extends JPA repository and extends another interface with abstract class defined?无论如何扩展JPA存储库并扩展另一个定义了抽象类的接口?
【发布时间】:2018-08-17 14:28:13
【问题描述】:

我想要一个 EntityRepository 扩展自 CrudRepository 接口和一个自定义的 CustomizedRepository 接口,该接口定义了一个抽象类。这可能吗?

我目前认为我可以将 EntityRepository 定义为

public interface EntityRepository extends EntityRepositoryCustom, CrudRepository<Entity, String> {
}

对于 EntityRepositoryCustom,我有

public interface EntityRepositoryCustom {
    public Page<Entity> findEntitisOnCondition(Entity t, Pageable pageable);
}

使用 EntityRepositoryCustomImpl

public class EntityRepositoryCustomImpl<Entity> extends SelfDefinedRepositoryImpl<Entity> {
    @Override
    protected String getSOmething() {
        ...
    }
}

而 SelfDefinedRepository 和 SelfDefinedRepositoryImpl 分别定义如下。

public interface SelfDefinedRepository<T, ID extends Serializable>  extends CrudRepository<T, ID> {
    Page<T> findEntitisOnCondition(T t, Pageable pageable);
}

public abstract class SelfDefinedRepositoryImpl<T, ID extends Serializable> implements SelfDefinedRepository<T, Serializable>{

    public Page<T> findEventsOnCondition(T t, Pageable pageable) {
        ...
        getSomething();
        ...
    }

    protected abstract String getSomething();

}

这个想法失败了。谁能帮助我实现一个存储库接口/实现,我可以拥有 JPA 存储库功能并且还可以扩展我的自定义抽象类?谢谢。

【问题讨论】:

  • 你这样做的目的是什么?您是否只想将常用方法分组到抽象类中并重用它们?
  • @Amit,我正在尝试在抽象类中获得“按条件进行通用搜索”功能。
  • 那么你可以使用组合而不是继承。
  • 你读过documentation吗?
  • 您使用的是什么版本的 Spring Data?还有它到底是怎么失败的?由于您有一个用于自定义实现的非抽象类,因此层次结构中的抽象类并不重要。

标签: spring spring-boot spring-data-jpa


【解决方案1】:

Add Custom Functionality to a Spring Data Repository

这可能对您的问题有所帮助。

【讨论】:

  • 文章链接不是答案
猜你喜欢
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2010-10-21
  • 2016-04-28
  • 2012-09-22
  • 1970-01-01
相关资源
最近更新 更多