【发布时间】:2019-07-10 10:50:24
【问题描述】:
我的 spring 数据 jpa 查询无法使用自定义 POJO 类。多次失败,出现以下异常
“org.hibernate.MappingException:未知实体: com.app.mycompany.AgileCenterServices.entities.ComponentDetailedInfo"*
尝试替换自定义的ComponentDetailedInfo.class,调用entityManager.createNativeQuery(componentQuery.toString())的过程中没有提及任何内容,但是查询后返回的对象列表无法转换为特定的POJO类。
@Override
public ComponentListResponsePaginated findComponentByProjectId(String projectId, Pageable pageable) {
logger.info(" Inside findComponentByProjectId() API in IssueComponentServiceImpl");
String componentQuery = "select c.*, u.fullname "
+ "from issue_component c "
+ "left join user u on c.component_lead = u.username "
+ "where "
+ "upper(c.project_id) = upper(" + projectId + ")";
List<ComponentDetailedInfo> compList = new ArrayList<ComponentDetailedInfo>();
try {
logger.info(" ************* Printing query ******************************* ");
logger.info(componentQuery.toString());
compList = entityManager.createNativeQuery(componentQuery.toString(), ComponentDetailedInfo.class) .setFirstResult(pageable.getOffset())
.setMaxResults(pageable.getPageSize())
.getResultList();
}
}
还尝试了以下
List<? extends Object> objList = null;
objList = entityManager.createNativeQuery(componentQuery.toString()) .setFirstResult(pageable.getOffset())
.setMaxResults(pageable.getPageSize())
.getResultList();
if(objList != null && objList.size() > 0) {
for(Object rec: objList) {
logger.info(" Printing Object ::: " + rec.toString());
compList.add((ComponentDetailedInfo)rec);
}
}
然而,compList 因
而失败java.lang.ClassCastException
返回的自定义查询应转换为传递给 entityManager.createNativeQuery 的特定类类型。但是,当我将课程传递给createNativeQuery() 时,我遇到了上述异常。
甚至尝试完全删除 createNativeQuery 中的类...
【问题讨论】:
标签: spring-boot spring-data-jpa