gpt4 book ai didi

java - Spring Data JPA 和 Projection 为 TupleConverter 获取 ConverterNotFoundException

转载 作者:行者123 更新时间:2023-11-29 06:50:26 26 4
gpt4 key购买 nike

有没有办法在 Spring Data 中编写和注册 TupleConverter 转换器?当我在 Repository 接口(interface)中有一个 @Query 注释并要求投影时,我得到了这个异常。

界面:

public interface ProjectRepository extends JpaRepository<Project, Integer> {
@Query("select p.projectId, p.projectName, p.techstack from Project p")
public List<ProjectItem> findAllForTest();
}

DTO:

public class ProjectItem {
private final Integer projectId;
private final String projectName;
private final String techstack;
@JsonCreator
public ProjectItem(
@JsonProperty("projectId") Integer projectId,
@JsonProperty("projectName") String projectName,
@JsonProperty("techstack") String techstack
) {
this.projectId = projectId;
this.projectName = projectName;
this.techstack = techstack;
}
public Integer getProjectId() {
return projectId;
}
public String getProjectName() {
return projectName;
}
public String getTechstack() {
return techstack;
}
}

异常

No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [project.item.ProjectItem]] with root cause

最佳答案

使用 JPQL Constructor Expression :

@Query("select new com.company.path.to.ProjectItem(p.projectId, p.projectName, p.techstack) from Project p")

关于java - Spring Data JPA 和 Projection 为 TupleConverter 获取 ConverterNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50220983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com