所以 - 我是新來的一般的hiberate和Java。我已經看到一些關於這個錯誤的線索,但沒有一個符合我的情況。我有一個簡單的JPQL查詢,如下所示,它應該從函數表/實體返回函數ID和名稱。此查詢保存在我的FunctionRepository.java中Java.lang.Object;不能被轉換成<hibernate entity>
@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
我想我很困惑,我要去哪裏錯了。
在此先感謝。
這工作!謝謝。我接受了這個答案,因爲它給了代碼完全修改的一小部分,但所有建議/迴應大致相同。 – RoflWaffle17