我正在開發一個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