【发布时间】: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