简体   繁体   中英

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 . 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

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 ?

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...

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. 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.

You can create ItemRepositoryCustom interface extending the ItemRepository interface, with each chart and table repos extending the ItemRepositoryCustom repo. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM