我使用
EclipseLink来运行一些Native sql.我需要将数据返回到POJO.我遵循
EclipseLink Docs的指示,但是我收到错误[Class]的描述符
查询列已被命名以匹配POJO的成员变量.我需要做一些附加的映射吗?@H_502_3@
POJO:@H_502_3@
public class AnnouncementRecipientsFlattenedDTO { private BigDecimal announcementId; private String recipientAddress; private String type; public AnnouncementRecipientsFlattenedDTO() { super(); } public AnnouncementRecipientsFlattenedDTO(BigDecimal announcementId,String recipientAddress,String type) { super(); this.announcementId = announcementId; this.recipientAddress = recipientAddress; this.type = type; } ... Getters/Setters
实体经理电话:@H_502_3@
public List<AnnouncementRecipientsFlattenedDTO> getNormalizedRecipientsForAnnouncement(int announcementId) { Query query = em.createNamedQuery(AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT,AnnouncementRecipientsFlattenedDTO.class); query.setParameter(1,announcementId); return query.getResultList(); }
解决方法
我发现您可以将本机查询执行的结果放入保存对象的数组列表中.然后可以遍历列表和Array元素并构建所需的Entity对象.
List<Object[]> rawResultList; Query query = em.createNamedQuery(AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT); rawResultList = query.getResultList(); for (Object[] resultElement : rawResultList) { AnnouncementDeliveryLog adl = new AnnouncementDeliveryLog(getAnnouncementById(announcementId),(String)resultElement[1],(String)resultElement[2],"TO_SEND"); persistAnnouncementDeliveryLog(adl); }