Skip to content

[clarification]: non-repository interfaces do not have primary entity types and so queries must explicit the entity type #1476

@gavinking

Description

@gavinking

Specification

@query on non-repository interfaces

I need clarification on ...

The TCK has this hidden treasure:

public interface DepartmentQueries<T> {

    @Query("where department = :dept order by name")
    List<T> findByDepartment(@Param("dept") String department);

    @Query("select count(*) where department = :dept")
    long countByDepartment(@Param("dept") String department);

    @Query("where name like :pattern order by name")
    List<T> searchByName(@Param("pattern") String pattern);
}
@Repository
public interface ProjectRegistry extends DataRepository<Project, Long>, DepartmentQueries<Project> {

    @Save
    void save(Project project);

    @Query("where budget > :min order by name")
    List<Project> fundedAbove(@Param("min") double min);
}

Let us now consider this gem of a method declaration in all its radiant glory:

    @Query("where department = :dept order by name")
    List<T> findByDepartment(@Param("dept") String department);

From the signature, we infer the query:

from T t where t.department = :dept order by t.name

But the generic type T cannot possibly have fields named department and name.

Oh, but a concrete argument to T might happen to be the class Project which just happens by complete coincidence to have such fields, so it's OK, right?

No. No, it's not okay. That's not how types work.

We should clarify in the spec that this is not supported, and we should delete the code from the TCK.

Additional information

UPDATE: Sorry! The TCK does not contain the code above! See comments below.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions