0

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.

1
  • When need an explanation first give us your exception error Commented Jan 8, 2016 at 9:49

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Hurricane you are just awesome. It worked. Thanks a loads.

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.