1

Is there any way to Cast hibernate Query result to Object instead of list array.

Query q1 = session.createQuery("from userTable where id ='1234'");

List userList = q1.list(); >> cast to object of UserBean class type ??

UserBean user = (UserBean)userList.get(0); 

System.out.println(user.getName);
System.out.println(user.getAge);
.....
System.out.println(user.getPhone);

UserBean class is mapped with a table name userTable and i wish to get the column value of one id which is primary key of the table.

Please see at line 2, List userList = q1.list() ?? can we cast/convert to object of type class anyhow.

2
  • 1
    Try UserBean user = (UserBean) q1.uniqueResult(); Commented Jul 22, 2013 at 10:05
  • @Reimeus Many thanks. it works. Please put this comment in answer so i would be able to accept your answer. Commented Jul 22, 2013 at 10:16

2 Answers 2

2

Why don't you use this?

UserBean user = (UserBean) q1.getSingleResult();
Sign up to request clarification or add additional context in comments.

3 Comments

but i am getting a compiler error, The method getSingleResult() is undefined for the type Query, any suggestion ?
hello priyank, may u please clarify bit more about the method ?
@user1010399 : Its a method of Query Interface. See docs.oracle.com/javaee/5/api/javax/persistence/… for more info. You shouldn't be getting compilation error.
1

Try this

UserBean user = (UserBean) q1.uniqueResult();

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.