I'm building a JPA criteria query but would like to use the setResultTransformer from Hibernate. As I understand I can first unwrap the JPA criteria query to a Hibernate query, then I can call the setResultTransformer API. This is fine, however in the transformTuple method in my result transformer class, the alias array is null even though I've specified alias when I build the criteria query. Why the alias name is missing?
Here is the code snippet. I'm using Hibernate 5.3.
Session session = HibernateUtil.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<MyPDO> root = cq.from(MyPDO.class);
cq.multiselect(
root.get("col1").alias("alias1"),
root.get("col2").alias("alias2")
);
TypedQuery<Tuple> query = session.createQuery(cq);
query.setHint(".hibernate.cacheMode", CacheMode.IGNORE);
MyResultTransformer myTransformer = new MyResultTransformer();
List<MyPDO> results = query.unwrap(.hibernate.query.Query.class)
.setResultTransformer(myTransformer)
.getResultList();
Here is the definition for my result transformer:
private static class MyResultTransformer implements ResultTransformer {
public Object transformTuple(Object[] tuple, String[] aliases) {
MyPDO row = new MyPDO();
//convert tuple to MyPDO here
.
.
.
return row;
}
public List transformList(List results) {
return results;
}
}
In the transformTuple method, the input parameter tuple has the proper data for the result row, however the String array aliases contains null values for each column. I expect the aliases should have the alias names I have specified when building the query. Why are the aliases missing?