0
  String mainQuery = "select x as CONDITION_"+i+" from xyz";
  SQLQuery sqlQuery = this.getSession().createSQLQuery(mainQuery);

from this query i will get allias column like

  CONDITION_x
  ------------
  value_x(anything)

here x is int value it will increment 0,1,2...

From this i want Json like ,

 [ 
   { 
     "CONDITION_0" :"value",
     "CONDITION_1" :"value"
   }
 ]

And this is in spring hibernate. Please help,TIA.

  • 1
    Possible duplicate of [How to generate auto increment field in select query](https://stackoverflow.com/questions/16555454/how-to-generate-auto-increment-field-in-select-query) – Book Of Zeus Feb 19 '19 at 04:28
  • This should help you [link](https://stackoverflow.com/questions/2605385/using-sql-column-names-in-hibernate-createsqlquery-result) – Gopi Feb 19 '19 at 04:31

1 Answers1

0

Use hibernate ResultTransformer's which converts SQLQuery result to Map<k,v> object with the alias column name in query as the k-key and row value as v-value.

String mainQuery = "select x as CONDITION_"+i+" from xyz";
SQLQuery sqlQuery = this.getSession().createSQLQuery(mainQuery);
List<Map<String,Object>> result = sqlQuery.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

By this you can get the json result as you expected.

Selvam M
  • 520
  • 4
  • 18