0

In the Hibernate, the former programmer implemented native query like this.

query = "select id from employee";
Query query = session.createSQLQuery(queryString).addScalar(scalarName, StringType.INSTANCE);
return query.list();

However, I would like to add one more field into query like

query = "select id, dept from employee";

If I do not add any code, it does return only id not including dept. I need multiple fields' value. I tried some references like http://www.journaldev.com/3422/hibernate-native-sql-query-example but still I can't fiture it out, does anybody have a solution for this quickly? :) Thank you so much!!

Anna Lee
  • 909
  • 1
  • 17
  • 37

1 Answers1

1

The simplest way to do it is:

query = "select id, dept from employee";
Query query = session.createSQLQuery(queryString);
return query.list();

Where query.list(); returns a list of Object[] and Object[0] ==> the id and Object[1] is the value of dept

Jens
  • 67,715
  • 15
  • 98
  • 113
  • I made it, i had to solve this quickly, so could not do enough research. anyway, it was so helpful. thank you so much! – Anna Lee Jul 19 '17 at 13:33
  • @AnnaLee You are welcome. If it is helpful, feeel free to upvote and/or accept the answer – Jens Jul 19 '17 at 13:34
  • @AnnaLee I have upvoted the question but you have not even accepted the answer. It is wrong to not accept a correct answer - especially when you agree that it is correct. – likejudo Mar 09 '22 at 13:49