I have a rather complicated query (there are too many nested levels for HQL or Criteria queries), so I wrote it as SQLQuery. I would really like to use AliasToBeanResultTransformer to convert my results to a list, but I am having some problems. I have included code snippets below what I have right now.
When I log the results for a converted query, I see that the transformer is actually creating a list, however all the fields in each AdvancedClauseSearchResultDTO are null. I guess this means that I'm doing something wrong with aliasing ... that AliasToBeanResultTransformer cannot find the correct setters for the call. However, the AdvancedClauseSearchResultDTO class has public setters for each of the columns that I used in my sql row. If it was a Criteria query, I would use predictions to define an alias for each column to be returned, but I'm not sure how to accomplish the same thing using SQLQuery.
Any tips on how to get the aliases set to use ResultTransformer? I saw some limited documentation saying that using a method like aliasName should work, but it doesn't seem to be for me.
Beginning of the query string definition snippet, pay attention to the "like" definitions of the alias
StringBuffer clauseBaseQuery = new StringBuffer();
clauseBaseQuery.append("select ");
clauseBaseQuery.append(" clauseDetail.clause_detail_id as clauseDetailId,");
clauseBaseQuery.append(" clauseDetail.clause_id as clauseId,");
clauseBaseQuery.append(" providers.provider_name as provider, ");
clauseBaseQuery.append(" products.product_name as product, ");
Creating SQLQuery and tuning resultTransformer
Query query = session.createSQLQuery(clauseBaseQuery.toString());
query.setResultTransformer(new AdvancedClauseSearchResultTransformer());
return (List<AdvancedClauseSearchResultDTO>)query.list();
AdvancedClauseSearchResultTransformer class (uses AliasToBeanResultTransformer and then does some extra processing):
class AdvancedClauseSearchResultTransformer implements ResultTransformer {
ResultTransformer aliasTransformer = Transformers.aliasToBean(AdvancedClauseSearchResultDTO.class);
@Override
public List transformList(List list) {
log.debug("transforming CLAUSE results");
List<AdvancedClauseSearchResultDTO> result = aliasTransformer.transformList(list);
for (AdvancedClauseSearchResultDTO dto : result) {
log.debug("dto = " + dto);
String status = null;
Date effectiveDate = dto.getEffectiveDate();
Date terminationDate = dto.getTerminationDate();
Date now = new Date(System.currentTimeMillis());
if (now.before(effectiveDate)) {
status = "Pending";
} else if (now.after(terminationDate)) {
status = "Terminated";
} else {
status = "Active";
}
dto.setStatus(status);
if (StringUtils.isNotEmpty(dto.getReasonForAmendment())){
dto.setAmended(Boolean.TRUE);
}else{
dto.setAmended(Boolean.FALSE);
}
}
return result;
}
@Override
public Object transformTuple(Object[] os, String[] strings) {
Object result = aliasTransformer.transformTuple(os, strings);
return result;
}
}