Declare field in class that extends multiple interfaces

Viewed 83

I'm working with QueryDSL and i'm trying to create something "general" to query all my entities, without write predicates for each of them.

Because I'm using JPA, I have interfaces that extends JpaRepository, and because I'm using QueryDSL the interface extends too QueryDslPredicateExecutor

The interface is something like this:

public interface MyUserRepository extends JpaRepository<MyUser, Long>, 
  QuerydslPredicateExecutor<MyUser> {

Now I have a generic service that have a field to access JPA repository:

private JpaRepository<E, ID> repository;

This works but I cant use the method findAll that is described in QuerydslPredicateExecutor.

So the question is, there is a syntax to change private JpaRepository<E, ID> repository; and say that repository extends JpaRepository and QuerydslPredicateExecutor.

More in general a syntax to say that a field extends multiple interfaces, so when I use it i can access to all methods.

Thanks

1 Answers

You could have added the generic type argument to your MyUserRepository interface.

public interface MyUserRepository<E, ID>  extends JpaRepository<E, ID>, 
  QuerydslPredicateExecutor<ID>

Then you could declare your repository object as

private MyUserRepository<E, ID> repository;
Related