ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2019-06-11 15:48:24  阅读:409  来源: 互联网

标签:java orm jpa 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,orm,jpa,eclipselink,jpa-2-1
来源: https://codeday.me/bug/20190611/1219621.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有