我需要连接一些表并从中返回结果。 查询位于 JpaRepository 中
@Query(value = " select mt.* from user_data d n" +
" join user_data_marks dm on dm.user_data_marks_id=d.idn" +
" join point mt on mt.point_id= dm.user_data_point_id" +
" where mt.point_time = :pointTime ", nativeQuery = true)
List<Point> findAllByPointTime(@Param("pointTime") LocalDate today);
但是当查询在测试中执行时,我得到:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.project.application.model.user.Point] for value '{1, 2019-12-26, ADVANCED}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.project.application.model.user.Point].
我想问题出在查询本身,但无法弄清楚为什么它不想返回 Point 类型的对象?
更新: 点实体:
@Data
@Entity
@Table(name = "point")
public class Point{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "point_id")
private long id;
@Column(name = "point_type")
@Enumerated(EnumType.STRING)
private PointType pointType;
@Column(name = "point_time")
private LocalDate pointTime;
select mt.* from user_data d
为什么不呢:
select p from Point p
?
您需要使用@SqlResultSetMapping
。使用示例在提供的 JavaDoc 中。
除此之外,查询看起来非常简单,你可以将其重写为 JPQL 查询,这将给你带来平台独立性、速度等好处,因为它将使用准备好的语句和错误检查,因为 Spring Data JPA 默认在引导时验证每个 JPQL 查询。