Anna Lee
Anna Lee

Reputation: 951

How to extract more than one field value from native query in hibernate?

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!!

Upvotes: 0

Views: 1086

Answers (1)

Jens
Jens

Reputation: 69480

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

Upvotes: 1

Related Questions