2

I have a Spring repository that I try to customize. I use fragments because I need a spring bean wired to perform my logic.

Here is the interface I'm using to override the standard logic:

@NoRepositoryBean
public interface VerifyingRepo<T, ID> extends CrudRepository<T, ID>, PagingAndSortingRepository<T, ID> {
    @Override
    <S extends T> S save(S s);
}

Here is its implementation:

public class VerifyingRepoImpl<T, ID> extends SimpleJpaRepository<T, ID> implements VerifyingRepo<T, ID> {
    private final MyBean myBean;

    public VerifyingRepoImpl(MyBean myBean, JpaEntityInformation<T, ?> entityInformation, EntityManager em) {
        super(entityInformation, em);
        this.myBean= myBean;
    }

    @Override
    @Transactional
    public <S extends T> S save(S entity) {
        myBean.customLogic(entity);
        return super.save(entity);
    }
}

Here is my repository I'm trying to use:

@RepositoryRestResource
public interface MyRepo extends VerifyingRepo<MyEntity, Long> {
}

I expect my custom logic to fire when I'm invoking the save() method of MyRepo (when I'm posting a dto to it's representing path). However, Spring instantiates MyRepo as SimpleJpaRepository and invokes only it's save()

Why doesn't Spring add the fragment with my custom save() to the resulting repository instance? It doesn't even instantiate my class.

1 Answer 1

0

Your interface should be declared as

@NoRepositoryBean
public interface VerifyingRepo<T, ID> extends JpaRepository<T, ID> {
    @Override
    <S extends T> S save(S s);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.