Skip to content

Whether Class-based Projections does not support use with @query #2558

@immadolf

Description

@immadolf

As shown in the figure, it is mentioned in the official document that Class-based Projections cannot be used with native query, while native query is described as setting the nativeQuery flag to true.

However, when I mixed @query with Class-based Projections, I saw an error org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type.

Here is my code:

public interface PersonRepository extends Repository<Person, Long> {

    @Query("select p from Person p where p.firstName = ?1")
    PersonDto findByFirstName(String firstName);

}

@Entity
public class Person {
    @Id
    private Long id;
    private String firstName;
    private String lastName;
    @OneToOne(mappedBy = "person")
    private Address address;

   // getter and setter ...
}

public class PersonDto {
    private final String firstName;
    private final String lastName;

    public PersonDto(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonDto personDto = (PersonDto) o;
        return Objects.equals(firstName, personDto.firstName) && Objects.equals(lastName, personDto.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(firstName, lastName);
    }
}

图片

图片

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions