繁体   English   中英

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

[英]JPA @SqlResultSetMapping not able to handle empty sql result to be mapped to empty POJO - instead an exception is thrown

我使用JPA 2.1(EclipseLink的供应商)@SqlResultSetMapping到SQL查询映射到实体没有POJO,它工作时的SQL结果不是空的,但是当我的表是空的 空表与空值

我的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自动处理空结果吗? 或任何其他建议?

代码示例:

  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")

看来Eclipselink上有这个开放的bug。 我正在使用2.6.3而且我也面临同样的问题 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=484276

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM