Quarkus: spring data - support manual method implementations in intermediate interfaces

Created on 21 Oct 2020  路  5Comments  路  Source: quarkusio/quarkus

Description
We use JpaRepository throughout our project. Works like a charm.
We besically want to extend all our JpaRepository subtypes with the same method. As this method is "generically" dependent on the entity class, we cannot simply implement a fragment that forwards to entitymanager:

public class FragmentImpl<T> implements Fragment<T> {
    @Inject EntityManager em;

    public T findMandatoryById(Long id) {
        return Optional.ofNullable(em.find([TODO class not available here], id)).orElseThrow(() -> ...);
    }
}

As we want to have that default method available in all our repositories, it would be great if this is supported:

public interface ExampleRepository extends CustomRepository<ExampleEntity> {}
public interface PersonRepository extends CustomRepository<PersonEntity> {}
public interface CarRepository extends CustomRepository<CarEntity> {}
[...100 more...]

@NoRepositoryBean
public interface CustomRepository<T> extends JpaRepository<T, Long> {
    default T findMandatoryById(Long id) {
        return findById(id).orElseThrow(() -> () -> new IllegalStateException("not found: " + id));
    }
}

This way, we wouldn't have to copy the ame method to all our repository interfaces.

Currently:

  1. The repository bean for ExampleRepository etc. is simply not created, leading to UnsatisfiedResolutionException: Unsatisfied dependency for type ExampleRepository.
  2. A manually implemented default method in a repository is overwritten by quarkus. For the above mehod signature, it tries to do so and then fails.

Implementation ideas

  1. support intermediate interfaces for spring data repositories
  2. skip existing default methods during implementation generation. Maybe by introducing a marker interface for these methods, maybe by directly differentiating between interface method declarations and default implementations.

/cc @geoand

arespring kinenhancement

Most helpful comment

yes, I will take a look asap! thank you @geoand

All 5 comments

/cc @geoand

cc @aureamunoz in case you find this interesting

yes, I will take a look asap! thank you @geoand

Excellent, thanks!

@aureamunoz this looks great 馃殌

Was this page helpful?
0 / 5 - 0 ratings