所以 - 我对 hiberate 和 Java 总体来说是新手。我已经看到了关于此错误的几个线程,但似乎没有一个适合我的情况。我有一个简单的 JPQL 查询,如下所示,它应该从函数表/实体中返回函数 ID 和名称。该查询保存在我的 FunctionRepository.java 中
@Query("SELECT func.functionId, func.functionName"
+ " FROM Function func")
List<Function> findItAll();
-----below is my FunctionService.java-------
public ArrayNode getAllFunctions() {
ArrayNode json = null;
try {
List<Function> functions = (List<Function>) functionRepository.findItAll();
json = crudEntitiesToJson(functions);
} catch (CorruptDataException cdEx) {
logger.error(cdEx.getMessage());
}
return json;
}
“crudEntitiesToJson”方法如下;
private ArrayNode crudEntitiesToJson(Iterable<Function> entities) throws CorruptDataException {
ArrayNode result = new ArrayNode(JsonNodeFactory.instance);
for (Function entity : entities) {
result.add(FunctiontoJson(entity));
}
return result;
}
这一切都是由我的 FunctionController.java 启动的
该项目构建并运行良好,但是当我尝试访问启动该查询的端点时,出现以下错误:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.ge.starfish.entity.Function
我想我很困惑我哪里出错了。
提前致谢。
最佳答案
更改
@Query("SELECT func FROM Function func")
List<Function> findItAll();
检索所有函数而不是具有两个属性的数组。它也可以工作:
@Query("FROM Function")
List<Function> findItAll();
关于Java.lang.Object;无法转换为 <hibernate 实体>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812684/