I'm returning the results of a SQL query in JSON format. The result is ok, but the keys (columns specified in the query) are missing. Do I have to associate a class with the query in order to get key names? I don't think it's the Jackson Mapping since when I use it explicitly (object mapper instead of letting Spring handle it in the @ResponseBody) I get the same result.
Spring 3.2.1, Hibernate 3.6. Using Hibernate createSQLQuery to get results from 5 tables.
List<EvalMasterEvalDetail> details = session.createSQLQuery(query).list();
Result looks like:
[[61,"Conference","CME Conference"],[42,"Lecture","fellow lecture"]]
Should be
[[{"detail_id":61, "event_type":"Conference", "event_name":"CME Conference"}],
[{"detail_id":42, "event_type":"Lecture", "event_name":"fellow lecture"}]]
By default SQL query in Hibernate returns a list of scalar values (for single column in select) or a list of Object[] (for multiple columns).
You have the latter case. List<EvalMasterEvalDetail> in this case doesn't mean that list contains instances of EvalMasterEvalDetail, because list() returns a raw List, so that you have unchecked conversion.
If each tuple of result represents a mapped entity (or multiple mapped entities), you can use addEntity() and addJoin() to convert them to entities.
If each tuple of result represents an arbitrary (non-mapped) class, you can use ResultTransformer (such as AliasToBeanResultTransformer).
Also you can convert Object[]s to target objects manually (useful in complex cases).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With