1
Query query = this.getSession().createSQLQuery(sqlQuery); query.setResultTransformer(new AliasToEntityMapResultTransformer()); List results = query.list(); 

In above code, results contains elements of Map and I am able to get the column name by key of Map but as you know query.list() returns the elements of HashMap thats why the ordering of column name not in sequence and I want the column name odering baseed on sql-query sequence.

2 Answers 2

2

This is a very old question, but it is a bit vexing as to why there isn't something like this in the transformers already.

Running the query with the transformer:

Query sqlQuery = session.createSQLQuery(sqlStatement); sqlQuery.setResultTransformer(AliasToEntityLinkedMapResultTransformer.INSTANCE); List results = query.list(); 

For the transformer, I extended BasicTransformerAdapter as opposed to AliasToEntityMapResultTransformer.

import org.hibernate.transform.BasicTransformerAdapter public class AliasToEntityLinkedMapResultTransformer extends BasicTransformerAdapter implements Serializable { public static final AliasToEntityLinkedMapResultTransformer INSTANCE = new AliasToEntityLinkedMapResultTransformer(); private AliasToEntityLinkedMapResultTransformer() { } public Object transformTuple(Object[] tuple, String[] aliases) { Map result = new LinkedHashMap(tuple.length) for (int i = 0; i < tuple.length; i++) { String alias = aliases[i] if (alias != null) { result.put(alias, tuple[i]) } } return result; } private Object readResolve() { return INSTANCE } public boolean equals(Object other) { return other != null && AliasToEntityLinkedMapResultTransformer.class.isInstance(other) } public int hashCode() { return getClass().getName().hashCode() } } 

Nothing revolutionary or straightforward, but there it is.

Sign up to request clarification or add additional context in comments.

Comments

1

Implement your own AliasToEntityLinkedMapResultTransformer, by using the code of AliasToEntityMapResultTransformer as an example, but using a LinkedHashMap instead of a HashMap. LinkedHashMap preserves insertion order.

5 Comments

How would i implement own AliasToEntityLinkedMapResultTransformer
Take the source code of AliasToEntityMapResultTransformer, change the package and class names, and use a LinkedHashMap instead of a HashMap.
class AliasToEntityLinkedMapResultTransformer extends AliasToEntityMapResultTransformer { public Object TransformTuple(Object[] tuple, String[] aliases) {Map result = new LinkedHashMap(tuple.length); for (int i = 0; i < tuple.length; i++) { String alias = aliases[i]; if (alias != null) { result.put(aliases[i], tuple[i]);} } return result;} public List TransformList(List collection) { return collection; } } i have created this class and called its object as query.setResultTransformer(new AliasToEntityLinkedMapResultTransformer()); but same problem exists
Show us the query, add traces to show what the tuple and aliases array contain, and show us the result of those traces.
@JBNizet why would we need a LinkedHashMap or the order in column|value pairs? Sorry if it sounds dumb but I am not able to figure it out why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.