0

When I'm using this I'm getting java.lang.ClassCastException:
java.lang.String cannot be cast to com.transgenomic.domain.Note error showing

public List<Note> getNoteDescription(Long userId) {
    Session session=sessionFactory.openSession();
    Transaction transaction=session.beginTransaction();
    SQLQuery query=session.createSQLQuery("SELECT n.description"
         + " FROM notes n, users u, tasks t "
         + " where u.userId=t.assignTo AND t.taskId=n.taskId");

    List<Note> notes=query.list();
    System.out.println("***********"+notes);
    transaction.commit();
    session.close();
    return notes;
}
1
  • It is easy with Hibernate Criteria.If you post your mapping we can help you easyly Commented May 22, 2015 at 9:12

2 Answers 2

1

Hibernate returns a raw List from the Query.list method.

You are doing an unsafe cast from List<?> to List<Note> - this cannot be avoided.

The only way to make the error go away is:

List<Note> list = new LinkedList<>();
for(Note n : query.list()) {
    list.add((Note));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Either:
Change your select from SELECT n.description into SELECT n
or
change your method return type and type of notes from List<Note> into List<String>

Assuming type of "description" is String, using SELECT n.description will return that field only, hence query.list() will return List, this doesn't match the expected List<Note>

Using SELECT n means returning the whole object

Comments

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.