Skip to content

Allow generic type parameter (no-args subclassing) for QuerydslRepositorySupport #4300

Description

@HarryJhin

QuerydslRepositorySupport still needs the domain class passed into the constructor:

class MemberRepositoryImpl extends QuerydslRepositorySupport {
    MemberRepositoryImpl() { super(Member.class); }
}

The rest of Spring Data already infers the domain type from the repository generics
(DefaultRepositoryMetadata reads getTypeArguments() off Repository<T, ID>), so this base class
seems to be the one place where you still have to pass the type by hand.

I'd like to subclass without that constructor argument, taking the type from a generic parameter instead:

class MemberRepositoryImpl extends QuerydslRepositorySupport<Member> {}

The type could be resolved with GenericTypeResolver (or the ResolvableType / TypeInformation
utilities that are already used elsewhere), and getBuilder() could return PathBuilder<Member> instead
of PathBuilder<?>. It would also drop the constructor boilerplate and rule out passing a wrong
Class<?> by accident.

Would it be possible to add this while keeping the existing super(Class) constructor for compatibility?
I saw #718 mention the explicit constructor was introduced to pin getBuilder() to a single domain type,
but that seems equally achievable through the generic parameter, so I'm not sure whether the explicit form
is a hard requirement or just the original default.

(I know QuerydslPredicateExecutor already covers a lot of cases. This is specifically about the
QuerydslRepositorySupport base used for arbitrary join/projection queries, where the constructor is the
remaining bit of friction.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions