gpt4 book ai didi

java.lang.ClassCastException : java. lang.String 无法转换为 [Ljava.lang.Object; - 使用 hibernate 从数据库中获取查询

转载 作者:行者123 更新时间:2023-12-01 16:52:01 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取不同的记录。这是我的尝试

@SuppressWarnings("unchecked")
@Override
public List<Employee> getAllEmployees(String employeeName) {
String query = "SELECT DISTINCT e.name FROM Employees e WHERE e.name like '%"+ employeeName +"%'";
List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);
List<Employee> employees = new ArrayList<Employee>();
for(Object[] employeeObject: employeeObjects) {
Employee employee = new Employee();
long id = ((BigInteger) employeeObject[0]).longValue();
int age = (int) employeeObject[1];
String name = (String) employeeObject[2];
float salary = (float) employeeObject[3];
employee.setId(id);
employee.setName(name);
employee.setAge(age);
employee.setSalary(salary);
employees.add(employee);
}
System.out.println(employees);
return employees;
}

这是发生错误的行

for(Object[] employeeObject: employeeObjects) { //line 65

当我尝试搜索记录时出现此错误

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
com.ranga.dao.impl.EmployeeDAOImpl.getAllEmployees(EmployeeDAOImpl.java:65)
com.ranga.service.impl.EmployeeServiceImpl.getAllEmployees(EmployeeServiceImpl.java:48)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy23.getAllEmployees(Unknown Source)
com.ranga.controller.EmployeeController.searchEmployee(EmployeeController.java:73)

请问哪里出了问题?

最佳答案

您的查询是:

SELECT DISTINCT e.name FROM Employees e WHERE e.name like '%"+ employeeName +"%'

(未参数化。糟糕。非常糟糕。顽皮的开发人员!)

因此您选择唯一的 e.name来自Employees - 这意味着你会得到一些名字。

因此我怀疑hibernateUtil.fetchAll返回 List<String> .

你会:

List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);

我怀疑这是来自 List<?> 的不安全转换或List (原始)。

然后你就这样做

for(Object[] employeeObject: employeeObjects) {
...
}

此时,Java 会将项目转换到 List 中。到所需的类型,即 Object[] 。因此你的错误。

关于java.lang.ClassCastException : java. lang.String 无法转换为 [Ljava.lang.Object; - 使用 hibernate 从数据库中获取查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com