【发布时间】:2019-05-22 17:16:50
【问题描述】:
MySQL 数据库包含一个包含两列的表。我正在尝试生成一个列表以仅显示一列的数据。我已经尝试过@Query,但它抛出了 Casting 错误。还有其他可能的选择吗?在这里感谢这个新手的一些指导。谢谢!
当我删除 @Query 时,两列都正确地从 mySQL 中提取,因此连接没有问题。
@Query("select p.date from PublicHoliday p" )
List<PublicHoliday> findAll();
List<PublicHoliday> phList = phRepo.findAll();
来自网络的错误消息:
Failed to convert from type [java.util.ArrayList<?>] to type [@org.springframework.data.jpa.repository.Query java.util.List<sg.edu.nus.lms1.model.PublicHoliday>] for value '[2019-05-19, 2019-05-20, 2019-06-04, 2019-08-09]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.data.jpa.repository.Query sg.edu.nus.lms1.model.PublicHoliday]
即使我在代码中将其更改为 ArrayList:
Failed to convert from type [java.util.ArrayList<?>] to type [@org.springframework.data.jpa.repository.Query java.util.ArrayList<sg.edu.nus.lms1.model.PublicHoliday>] for value '[2019-05-19, 2019-05-20, 2019-06-04, 2019-08-09]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.data.jpa.repository.Query sg.edu.nus.lms1.model.PublicHoliday]
【问题讨论】:
-
您需要一个 RowMapper 来接收 java.sql.Date 并返回一个 PublicHoliday。为什么需要 JPA?从 JdbcTemplate 开始。