2016-05-30 11 views
3

を投げている私はそれがの作品、なしエンティティPOJOにSQLクエリをマッピングするために@SqlResultSetMapping JPA 2.1(EclipseLinkのベンダー)を使用していますSQL結果は空ではありませんが、私のテーブルが空の場合empty table with nullsJPA @SqlResultSetMapping - 代わりに例外が

は、私のPOJOの建設は失敗して例外が発生したとき:空結果がのためにJPAによって自動的に処理されなければならないことを

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. 

マイ期待私の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 
) 
+0

をあなたがどんな解決策を見つけますか? – vels4j

答えて

4

をあなたはこのようなタイプを指定する必要があります。

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

関連する問題