Subramanya K
Subramanya K

Reputation: 1

org.hibernate.SQLQuery output to an object

I have a table by name Person and it has only one row data. I dont want to take it in list(). Can I get the data of Object type. Below is my code snippet

Session session = this.sessionFactory.openSession();
String sql = "SELECT * FROM PERSON";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Person.class);
List<Person> list = query.list();

===================================

I want the above code snippet to work as below:

Session session = this.sessionFactory.openSession();
String sql = "SELECT * FROM PERSON";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Person.class);
Person p = query;

which is throwing error. Please help me out on fixing the issue.

Upvotes: 0

Views: 1631

Answers (1)

hurricane
hurricane

Reputation: 6724

query is result of your session select. query.list() is your results. and you can get with query.list().get(0);

You can cast query list to personList;

List<Person> list = query.list();
return list.get(0);

You can just cast your result to person;

Person p = (Person) query.list().get(0);

Upvotes: 2

Related Questions