Reputation: 1000
Im getting the following error:
"No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [de.hiqs.project.DTO.ProjectWithoutEmployeeDTO]"
The query which causes the error:
@Query("SELECT project.id, project.name, project.budget, " +
"project.description, project.isArchived, project.archivedDate, " +
"project.creationDate, project.customer.name AS customerName " +
"FROM Project project")
List<ProjectWithoutEmployeeDTO> findAllWithoutEmployee();
My entity:
@Data
@Entity
@ToString(exclude = {"employees"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(unique = true)
private String name;
private Integer budget;
private String description;
private Boolean isArchived;
private LocalDate archivedDate;
private LocalDate creationDate;
@NotNull
@ManyToOne
private Customer customer;
@OneToOne(cascade = CascadeType.ALL)
private DefaultDailyEntrySettings defaultDailyEntrySettings;
@ManyToMany
@JoinTable(
name = "employee_projects",
joinColumns = @JoinColumn(name = "project_id"),
inverseJoinColumns = @JoinColumn(name = "employee_id")
)
private List<Employee> employees;
The DTO:
@Data
public class ProjectWithoutEmployeeDTO {
private long id;
private String name;
private Integer budget;
private String description;
private Boolean isArchived;
private LocalDate archivedDate;
private LocalDate creationDate;
private String customerName;
}
So it seems like he cant convert the query result to ProjectWithoutEmployeeDTO
. I thought it would work out automatically because the class has the exact same named attributes as the query result, but it seems like it doesnt. Whats the best way to convert the result efficiently? I could return a list of objects instead of a list of ProjectWithoutEmployeeDTO
and convert it manually inside the service, but that doesnt seem like the best way.
Edit: I managed to do it with
@Query("SELECT new classpath.ProjectWithoutEmployeeDTO(project.id, project.name, project.budget, " +
"project.description, project.isArchived, project.archivedDate, " +
"project.creationDate, project.customer.name) " +
"FROM Project project")
List<ProjectWithoutEmployeeDTO> findAllWithoutEmployee();
And then just add that constructor to the DTO or let Lambok do it automatically with @AllArgsConstructor
Upvotes: 0
Views: 241
Reputation: 36143
Your query should use the constructor expression:
@Query("SELECT NEW yourpackage.ProjectWithoutEmployeeDTO(project.id, project.name, project.budget, " +
"project.description, project.isArchived, project.archivedDate, " +
"project.creationDate, project.customer.name AS customerName) " +
"FROM Project project")
Please note that ProjectWithoutEmployeeDTO must be the fully qualified
Upvotes: 1