Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
immadolf opened this issue May 31, 2022 · 2 comments
Closed

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

immadolf opened this issue May 31, 2022 · 2 comments
Assignees
Labels
type: documentation A documentation update

Comments

@immadolf
Copy link

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);
    }
}

图片

图片

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 31, 2022
@mp911de mp911de transferred this issue from spring-projects/spring-data-commons Jun 1, 2022
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 25, 2022
@gregturn
Copy link
Contributor

I understand that the reference documentation implied the code you wrote should work just fine. I updated them to clarify exactly how class-based DTOs work with Spring Data JPA, hopefully giving you the ability to leverage you PersonDto within an @Query annotation!

gregturn added a commit that referenced this issue Sep 29, 2022
gregturn added a commit that referenced this issue Sep 29, 2022
@gregturn
Copy link
Contributor

Backported to 2.7.x and 2.6.x.

@gregturn gregturn added this to the 3.0 RC1 (2022.0.0) milestone Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants