org.springframework.core.convert.ConversionFailedException:无法转换为类型

huangapple go评论77阅读模式
英文:

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(&quot;SELECT S.studentId, S.studentName, S.studentSurname, S.studentAge, S.entryYear,&quot; +
        &quot;S.graduateYear, S.facultyName, G.groupName FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId&quot;)
    List&lt;StudentDTO&gt; getAllStudentDtos();

And when I launch program and try to get this data from controller :

  @GetMapping(&quot;students&quot;)
public String showStudents(Model model) {
    List&lt;StudentDTO&gt; students = studentService.getAllStudentDtos();
    model.addAttribute(&quot;students&quot;, students);
    return &quot;studentViews/studentsPage&quot;;
} 

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 &#39;{1, Alexandr, Platonchik, 37, 1999, 2004, journalism, BIKS}&#39;; 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&lt;StudentDTO&gt; as you thought. Spring repositories are binded to an @Entity, in your case Student, so when you use the method return as List&lt;Student&gt; 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(&quot;SELECT new StudentDTO(S.studentId, S.studentName) FROM Student S LEFT JOIN Group G ON G.groupId=G.groupId ORDER BY S.studentId&quot;)
List&lt;StudentDTO&gt; 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&lt;StudentDTO&gt; students = studentService.getAllStudentDtos().stream().map(student -&gt; StudentDTO.toDto(student)).collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月6日 19:36:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63763707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定