【问题标题】:Spring Data JPA native query result bindingSpring Data JPA 原生查询结果绑定
【发布时间】:2017-12-13 14:22:19
【问题描述】:

实体类:

@Entity
@SqlResultSetMapping(
    name="hourMapping",
    classes=@ConstructorResult(
            targetClass=Representation.class,
            columns={
                    @ColumnResult(name="hour", type=BigDecimal.class),
                    @ColumnResult(name="transactions", type=BigDecimal.class)
            }
            )
    )

@NamedNativeQuery(name="MyEntity.reportByHour", query="SELECT hour,SUM(tran_per_hour) AS transactions FROM MY_ENTITY GROUP BY hour ORDER BY hour"
    ,resultSetMapping="hourMapping")

@Table(name="MY_ENTITY")
public class MyEntity implements Serializable {

Pojo 类:

@Data //Lombok
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Representation {

public Representation(BigDecimal hour, BigDecimal transactions) {
    this.hour = hour;
    this.transactions = transactions;
}

private BigDecimal hour;
private BigDecimal transactions;

存储库接口:

public interface MyEntityRepository extends JpaRepository<MyEntity, MyEntityPK> {
    List<Representation> reportByHour();
}

当我运行调用本机查询的端点时,出现异常:

值“{0, 198}”从类型 [java.lang.Object[]] 转换为类型 [com.representation.Representation] 失败;嵌套异常是 org.springframework.core.convert.ConverterNotFoundException:找不到能够从 [java.math.BigDecimal] 类型转换为 [com.representation.Representation] 类型的转换器

现在,如果我从本机查询返回“小时”字段(以及对 POJO 构造函数等的相关更改),它可以正常工作。

任何帮助表示赞赏。

【问题讨论】:

    标签: spring jpa spring-data-jpa


    【解决方案1】:

    好吧,虚惊一场。我的休眠依赖都搞砸了并导致冲突,因此导致上述异常。

    修复这些依赖问题后,效果很好!!

    长话短说:让 spring-boot-* 处理大多数休眠依赖项,而不是覆盖或管理您自己的。

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 2020-01-11
      • 2015-05-18
      • 2019-12-01
      • 1970-01-01
      • 2020-02-15
      • 2012-07-15
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多