Skip to content

Error initializing repository with query generated by method name that returns projection #2415

Closed
@plc010

Description

@plc010

Issue seems to have started with Spring Boot 2.6.3.

When a JpaRepository interface defines a query created by a method name that returns a projection the application will fail to start with the error below.

Failed to create query for method public abstract com.bug.demo.bugdemo.AgeProjection com.bug.demo.bugdemo.PersonRepository.findByFirstNameIgnoreCase(java.lang.String)! null; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.bug.demo.bugdemo.AgeProjection com.bug.demo.bugdemo.PersonRepository.findByFirstNameIgnoreCase(java.lang.String)! null

Repository:
`public interface PersonRepository extends JpaRepository<PersonEntity, Integer> {

Optional<AgeProjection> findByFirstName(String firstName);

}`

Adding a @query annotation to the method with the JPQL query below will successfully execute and return the projection.

`public interface PersonRepository extends JpaRepository<PersonEntity, Integer> {

@query("select p from PersonEntity p where p.firstName = ?1")
Optional findByFirstName(String firstName);

}`

Entity class:
`@Entity
public class PersonEntity {
@id
private Integer id;

private String firstName;

private String lastName;

private Integer age;

// getters/setters
}`

Age Projection:
public interface AgeProjection { Integer getAge(); }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions