0

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.

Community
  • 1
  • 1
sgt-hartman
  • 634
  • 6
  • 25

1 Answers1

0

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.

userJ
  • 181
  • 1
  • 4
  • I tried this solution but unfortunatelly it didnt worked. Maybe i did something wrong. I tried with a `ViewRepositoryCustom` interface, a `ViewRepositoryImpl` class, and, for the test, make the `ChartRepository` interface extend `ViewRepositoryCustom`. It only worked once i specialised `ViewRepositoryCustom` and `ViewRepositoryImpl` by simply renaming them `ChartRepositoryCustom` and `ChartRepositoryImpl`. So i doubt it works with abstract entity classes. – sgt-hartman Jan 31 '17 at 09:41
  • Hi, you edited answer got me on the way but i'm still not able to implement a custom repository. See my edit on the question. – sgt-hartman Feb 01 '17 at 11:19
  • @NoRepositoryBean this annotation should be on ItemBaseRepositoryCustom repo as this is custom repo and spring should not create proxy class for this as individually it has got no meaning to it. – userJ Feb 01 '17 at 11:57
  • this unfortunatelly doesn't change anything. – sgt-hartman Feb 01 '17 at 13:45