0
select id,name,salary,city,state,country from employee where name like '%a%';

I need to map above query result to String array, position 0 always id, position 1 always name ...... position 5 always country.

Using JPA or MyBatis is there a way we can dynamically map the select query values into fixed position string array ?

bakero98
  • 805
  • 7
  • 18
  • Yes, that would be the default result with JPA. Why don't you... try? – JB Nizet Aug 01 '19 at 19:58
  • @JBNizet so far I used to map for employee object, I need to may now string array, could you please share small example – Ramesh Mandri Aug 01 '19 at 20:11
  • there is so many ways to go about this. it would be hugely helpful you posted what you have attempted or trying to do. – denov Aug 01 '19 at 21:16
  • @jpganz18 & denov similar to the above scenario I have to map 100 + query results, each query select fields are different very few are common between the queries and number select columns for each query varies, altogether 400 - 500 columns. don't want to create a class for each query, so thinking to add index-based. Don't want to iterate and add values to the array, so looking is there a way I could map directly columns to the array. List array will have column values, the list will have row values. – Ramesh Mandri Aug 02 '19 at 05:08
  • check this code and give it a try https://stackoverflow.com/questions/7595328/jpa-2-0-native-query-results-as-map/46190527#46190527 – jpganz18 Aug 02 '19 at 07:06

2 Answers2

0

I've never personally used JPA, but - after reading through it a bit - believe this should be correct.

TypedQuery<Object[]> query = entityManager.createQuery(
  "SELECT id,name,salary,city,state,country FROM employee WHERE name LIKE '%a%'", Object[].class);
List<Object[]> results = query.getResultList();

where (Integer) results.get(index)[0] = id, (String) results.get(index)[1] = name, etc.

You can change Object[] to String[] if you would like an array of Strings.

omoshiroiii
  • 643
  • 5
  • 11
0

Why do you want to use String array?

It is better to use an object list instead. It will be easier to get city of an employee using employee.getCity() versus array[3].

It makes for more readable code as well.

To use an object list, you need to create a model for your employee and annotate with @Entity and create a repository for that entity. JPA documentation should help do this easily

Kalyan Chavali
  • 131
  • 1
  • 1
  • 13
  • similar to the above scenario I have to map 100 + query results, each query select fields are different very few are common between the queries and number select columns for each query varies, altogether 400 - 500 columns. don't want to create a class for each query, so thinking to add index-based. Don't want to iterate and add values to the array, so looking is there a way I could map directly columns to the array. List array will have column values, the list will have row values. – Ramesh Mandri Aug 02 '19 at 05:02
  • if that is what you want, it should be straight forward using resultSet. just do `stringArray[i] = resultSet.get(i)` – Kalyan Chavali Feb 21 '20 at 21:44