英文:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type
问题
以下是翻译好的内容:
我正在尝试从StudentRepository中的表中获取数据:
@Query("SELECT S.studentId, S.studentName, S.studentSurname, S.studentAge, S.entryYear," +
"S.graduateYear, S.facultyName, G.groupName FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId")
List<StudentDTO> getAllStudentDtos();
当我启动程序并尝试从控制器获取这些数据时:
@GetMapping("students")
public String showStudents(Model model) {
List<StudentDTO> students = studentService.getAllStudentDtos();
model.addAttribute("students", students);
return "studentViews/studentsPage";
}
我遇到了这个错误。
以下是所有的异常信息:
org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.Object[]] 转换为类型 [@org.springframework.data.jpa.repository.Query com.foxminded.university.dto.StudentDTO],值为 ' {1, Alexandr, Platonchik, 37, 1999, 2004, journalism, BIKS}';嵌套异常为 org.springframework.core.convert.ConverterNotFoundException: 找不到能够从类型 [java.lang.Integer] 转换为类型 [@org.springframework.data.jpa.repository.Query com.foxminded.university.dto.StudentDTO] 的转换器
英文:
I am trying to get data from table in the StudentRepository :
@Query("SELECT S.studentId, S.studentName, S.studentSurname, S.studentAge, S.entryYear," +
"S.graduateYear, S.facultyName, G.groupName FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId")
List<StudentDTO> getAllStudentDtos();
And when I launch program and try to get this data from controller :
@GetMapping("students")
public String showStudents(Model model) {
List<StudentDTO> students = studentService.getAllStudentDtos();
model.addAttribute("students", students);
return "studentViews/studentsPage";
}
I gain this error.
Here all exception :
org.springframework.core.convert.ConversionFailedException: Failed to convert from type
[java.lang.Object[]] to type [@org.springframework.data.jpa.repository.Query
com.foxminded.university.dto.StudentDTO] for value '{1, Alexandr, Platonchik, 37, 1999, 2004, journalism, BIKS}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [@org.springframework.data.jpa.repository.Query com.foxminded.university.dto.StudentDTO]
答案1
得分: 3
这个错误发生是因为你的仓库中的方法 getAllStudentDtos
返回了一个 Student
列表,而不是你所想的 List<StudentDTO>
。在 Spring 仓库中,与 @Entity
相关联,对于你的情况是 Student
,所以当你将方法返回类型设为 List<Student>
时,就会出现问题。
针对你的问题有两个可能的解决方案。第一个是在你的 StudentDTO
类中声明一个构造函数,参数要与你查询所需的字段完全对应,然后在 getAllStudentDtos
方法中使用它。
例如:
public StudentDTO(Long studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
}
...
@Query("SELECT new StudentDTO(S.studentId, S.studentName) FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId")
List<StudentDTO> getAllStudentDtos();
请注意,我在示例中只使用了两个字段。
或者你可以在 StudentDTO
中实现一个方法,将 Student
实体转换为 StudentDTO
,然后使用你的方法映射整个列表。
例如:
List<StudentDTO> students = studentService.getAllStudentDtos().stream().map(student -> StudentDTO.toDto(student)).collect(Collectors.toList());
英文:
This error happens because your method getAllStudentDtos
in the repository returns a list of Student
and not a List<StudentDTO>
as you thought. Spring repositories are binded to an @Entity
, in your case Student
, so when you use the method return as List<Student>
the problem occur.
There are two possible solutions to your problem. The first is declare a construtor in your StudentDTO
class with the exact field params you wanted for the query and use it in your getAllStudentDtos
.
eg:
public StudentDTO(Long studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
}
...
@Query("SELECT new StudentDTO(S.studentId, S.studentName) FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId")
List<StudentDTO> getAllStudentDtos();
Note that I used only two fields in the example.
Or you can implement a method in StudentDTO
that transforms a Student
entity to a StudentDTO
and then map the list using your method.
eg:
List<StudentDTO> students = studentService.getAllStudentDtos().stream().map(student -> StudentDTO.toDto(student)).collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论