user1010399
user1010399

Reputation: 2348

Cast hibernate Query result to Object of type class instead of list array

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.

Upvotes: 1

Views: 3282

Answers (2)

Reimeus
Reimeus

Reputation: 159864

Try this

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

Upvotes: 1

Priyank Doshi
Priyank Doshi

Reputation: 13161

Why don't you use this?

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

Upvotes: 2

Related Questions