[英]Does Spring Data Jpa supports repository inheritance and how to do?
I have a simple entity inheritance tree composed of the following: 我有一个由以下内容组成的简单实体继承树:
abstract class Item (id, ...)
class Chart extends Item (...)
class Table extends Item (...)
Each class has its own repository: ItemRepository
, ChartRepository
and TableRepository
. 每个类都有自己的存储库:
ItemRepository
, ChartRepository
和TableRepository
。 All three repositories are exposed . 这三个存储库都已暴露 。
Because of domain rules, I need to implement custom logic on delete, as explained here: http://docs.spring.io/spring-data/jpa/docs/1.10.3.RELEASE/reference/html/#repositories.custom-implementations 由于域规则的原因,我需要在删除时实现自定义逻辑,如下所述: http : //docs.spring.io/spring-data/jpa/docs/1.10.3.RELEASE/reference/html/#repositories.custom -实现
This custom delete logic concerns all entities in the inheritance tree (the abstract one and the two concretes). 这种自定义删除逻辑涉及继承树中的所有实体(抽象的两个实体)。
Is there a way to implement custom logic on the ItemRepository
and make the repositories of the children entities extend ItemRepository
? 有没有办法在
ItemRepository
上实现自定义逻辑并使ItemRepository
的存储库扩展ItemRepository
?
This would avoid duplicate of the same logic for each entity of the inheritance tree. 这将避免对继承树的每个实体重复相同的逻辑。 Without that this makes a lot of boilerplate classes :
没有这个,就会产生很多样板类:
EntityRepository
+ EntityRepositoryCustom
+ EntityRepositoryImpl
x 3 entities
= 9 classes
just to implement 1 ligne of code to be executed on delete... EntityRepository
+ EntityRepositoryCustom
+ EntityRepositoryImpl
x 3 entities
= 9 classes
仅用于实现要在删除时执行的1 ligne代码...
EDIT 编辑
The answer of user user7398104
got me on the way to implement things as explained here and there , but theses resources do not explain how to implement a custom repository on the @NoRepositoryBean
base repository. 用户的答案
user7398104
让我上实行事情解释的方式在这里和那里 ,但论文资源没有解释如何实现在一个自定义的库@NoRepositoryBean
基础库。 I tried the following without luck: 我没有运气就尝试了以下方法:
@NoRepositoryBean
public interface ItemBaseRepository<T extends Item> extends
CrudRepository<T, Integer>,
ItemBaseRepositoryCustom<T>
@RepositoryRestResource
public interface ItemRepository extends ItemBaseRepository<Item> {}
@RepositoryRestResource
public interface ChartRepository extends ItemBaseRepository<Item> {}
@RepositoryRestResource
public interface TableRepository extends ItemBaseRepository<Item> {}
public interface ItemBaseRepositoryCustom<T extends Item> {
public void delete (T i);
}
public class ItemBaseRepositoryImpl<T extends Item>
implements ItemBaseRepositoryCustom<T> {
public void delete (T i) {
// Custom logic
}
}
EDIT EDIT 编辑编辑
I tried to set @NoRepositoryBean
to ItemBaseRepositoryCustom as suggested on comments but it doesnt work either. 我试图设置
@NoRepositoryBean
到ItemBaseRepositoryCustom的意见的建议,但它不工作的。
You can create ItemRepositoryCustom interface extending the ItemRepository interface, with each chart and table repos extending the ItemRepositoryCustom repo. 您可以创建扩展ItemRepository接口的ItemRepositoryCustom接口,每个图表和表格存储库都可以扩展ItemRepositoryCustom存储库。 For this interface you can have a custom implmentation for the delete operation.
对于此接口,您可以为删除操作添加自定义实现。
EDIT : I just realised that this would not work. 编辑 :我只是意识到这将无法正常工作。
But the following approach would work. 但是以下方法会起作用。
interface CustomItemRepository<T extends Item> extends CrudRepository<T, Long>{ }
interface ChartRepository extends CustomItemRepository<Chart>{ }
interface TableRepository extends CustomItemRepository<Table>{ }
Then you can create a class for delete logic for CustomItemRepository. 然后,您可以为CustomItemRepository创建用于删除逻辑的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.