我正在开发一个Spring Hibernate的Web应用程序。从列表中获取列值时遇到错误。但是这个错误不断出现。请帮我放。提前致谢。java.lang.ClassCastException:java.lang.String不能转换为[Ljava.lang.Object;当试图通过休眠获得colum值到列表
@Repository
@Transactional
public class GetProjectsDaoImpl implements GetProjectsDao {
@Autowired
private HibernateUtilImpl hibernateutilimpl;
public List<Projects> getProjects() {
String sql = "select project_id from project";
List<Object[]> projectObjects = hibernateutilimpl.fetchAll(sql);
List<Projects> projectsList = new ArrayList<Projects>();
for(Object[] projectObject: projectObjects) {
Projects project = new Projects();
String id = (String) projectObject[0];
project.setProjectId(id);
projectsList.add(project);
}
return projectsList;
}
}
@Repository
public class HibernateUtilImpl implements HibernateUtil {
@Autowired
private SessionFactory sessionFactory;
public <T> Serializable create(final T entity) {
return sessionFactory.getCurrentSession().save(entity);
}
public <T> T update(final T entity) {
sessionFactory.getCurrentSession().update(entity);
return entity;
}
public <T> void delete(final T entity) {
sessionFactory.getCurrentSession().delete(entity);
}
@SuppressWarnings("rawtypes")
public <T> List<T> fetchAll(String query) {
return sessionFactory.getCurrentSession().createNativeQuery(query).list();
}
}
可能重复的[java classcast exception](https://stackoverflow.com/questions/2757034/java-classcast-exception) –
你试过在你的方法getProjects()中改变Object []为String吗? –
'hibernateutilimpl.fetchAll(sql)'返回一个列表'在你的情况下 –
Nathan