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:
ExampleRepository etc. is simply not created, leading to UnsatisfiedResolutionException: Unsatisfied dependency for type ExampleRepository.Implementation ideas
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
/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 馃殌
Most helpful comment
yes, I will take a look asap! thank you @geoand