1

I'm developing a spring web application with Hibernate. I have faced an error when getting column values from a table to a list. But this error keeps coming. Please help me put. Thanks in advance.

@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();
    }

}
4
  • 1
    Possible duplicate of java classcast exception Commented Aug 2, 2017 at 12:23
  • did you try in your method getProjects() change Object[] to String ? Commented Aug 2, 2017 at 12:26
  • hibernateutilimpl.fetchAll(sql) returns a list<String> in your case Commented Aug 2, 2017 at 12:34
  • by any chance is the id value coming out as an Integer and not a Object it could also be that the library is setting type based on column type information Commented Aug 2, 2017 at 12:34

1 Answer 1

2

I have following suggestions. First change:

List<Object[]> projectObjects = hibernateutilimpl.fetchAll(sql); 

to:

List<Object> projectObjects = hibernateutilimpl.fetchAll(sql);

Next, change:

for(Object[] projectObject: projectObjects) {
    String id = (String) projectObject[0];

to

for(Object projectObject: projectObjects) {
    String id = (String) projectObject;

The above change is needed because you are selecting only a single column. Object[] is used only when selecting more then one column.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. this works for a single column. but what if i want to select several columns?
Hi, for multiple columns the original code your wrote should work, let's say you have project_id and project_name, change your sql to fetch 2 columns, to get the value of second column do String project_name = (String) projectObject[1];
In addition to above if you are selecting all columns then you can do List <Projects> projectObjects if Projects has all the fields corresponding to columns in PROJECT table...
@Michael0x2a - Thanks for editing my answer, could you please tell me where can I find the instructions about how to format the content/code that we write.
@deepakl, panel where you edit or write the "Answer" has basic formatting toolbar; use that to make your post more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.