java - JPA @SqlResultSetMapping 无法处理要映射到空 POJO 的空 sql 结果 - 而是抛出异常

标签 java jpa orm eclipselink jpa-2.1

我正在使用 JPA 2.1(Eclipselink 供应商)@SqlResultSetMapping 将 sql 查询映射到无实体 POJO,当 sql 结果不为空时它有效,但当我的表为空时 empty table with nulls

我的 POJO 的构造失败,但出现异常:

 2016-05-30 11:44:17,154 [tp520017379-230] ERROR - Exception [EclipseLink-6177] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.QueryException
    Exception Description: The column result [st_agg] was not found in the results of the query.
    Query: ResultSetMappingQuery(name="nodeStatusAggQuery" sql="select max(status) as st_agg, max(clock_accuracy_health) as cac_agg, max(clock_analysis_health) as can_agg, max(ptp_net_analysis_health) as na_agg from sync_node where ncd_id = ? and type =?")
    javax.persistence.PersistenceException: Exception [EclipseLink-6177] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.QueryException
    Exception Description: The column result [st_agg] was not found in the results of the query.

我期望空结果应该由 JPA 自动处理,例如通过调用我的 POJO 的默认构造函数。我找到了一种通过捕获异常来解决问题的解决方法,但是我的问题我可以让 JPA 自动处理空结果吗?或任何其他建议?

Code Sample:

  public static SyncNodeStatusAggregation getMaxSeverStatusAndHealth(int ncdId, SyncProtocolType type){
    try {
      EntityManager em = ...
      Query aggregateQuery = em.createNamedQuery(SyncNodeDBImpl.NODE_STATUS_AGG_QUERY);
      aggregateQuery.setParameter(1, ncdId);
      aggregateQuery.setParameter(2, type.ordinal());
      return (SyncNodeStatusAggregation) aggregateQuery.getSingleResult();
      //javax.persistence.PersistenceException can be thrown when result is empty, JPA will not be able to map the result to object, thus we handle it be catching the exception
    } catch (PersistenceException e) {
      return new SyncNodeStatusAggregation(SyncNodeStatus.Ok, SyncHealthIndication.na, SyncHealthIndication.na, SyncHealthIndication.na );
    }
  }



@SqlResultSetMapping(
        name = SyncNodeDBImpl.RESULT_MAPPING_NAME,
        classes = {
                @ConstructorResult(
                        targetClass = SyncNodeStatusAggregation.class,
                        columns = {
                                @ColumnResult(name = "st_agg"),
                                @ColumnResult(name = "cac_agg"),
                                @ColumnResult(name = "can_agg"),
                                @ColumnResult(name = "na_agg"),
                        }
                )
        }
)
@NamedNativeQuery(
        name = SyncNodeDBImpl.NODE_STATUS_AGG_QUERY,
        query = "select max(status) as st_agg, max(clock_accuracy_health) as cac_agg, max(clock_analysis_health) as can_agg, max(ptp_net_analysis_health) as na_agg from sync_node where ncd_id = ?1 and type =?2",
        resultSetMapping =SyncNodeDBImpl.RESULT_MAPPING_NAME
)

最佳答案

你需要像这样指定类型:

@ColumnResult(name = "na_agg",type="Double.class")

关于java - JPA @SqlResultSetMapping 无法处理要映射到空 POJO 的空 sql 结果 - 而是抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524544/

相关文章:

java - 使用 Redis 发布/订阅 : how to make a subscription expire

java - 为 http ://schemas. opengis.net/wfs/1.1.0/wfs.xsd 生成 JAXB 类

java swing线程回调

database - 您将使用@OneToOne 关系的具体示例?

java - 带有 Map 的 JPA NullPointerException

java - 如何在 Hibernate 中使用 pooled-lo 优化器

java - 如何从 Alfresco 创建的自定义规则中检索文档 ID

java - OpenJPA 将具有 oneToMany 映射的字段视为 Blob

java - 为什么说SessionFactory是ConnectionProvider的客户端

c# - npoco.Insert<T> 在带有 OUT AutoIncrement 主键的表上失败