"group by " +
"childCategory.categoryId, childCategory.name, childCategory.parentCategory " +
"order by childCategory.name";
String whereClause;
Category param = null;
if (childCategory != null) {
whereClause = "childCategory = ?";
param = childCategory;
} else if (parentCategory != null) {
whereClause = "childCategory.parentCategory = ?";
param = parentCategory;
} else {
whereClause = "childCategory.parentCategory is null";
}
String hql = String.format(hqlTemplate, whereClause);
Query query = session.createQuery(hql);
if (param != null) {
query.setParameter(0, param);
}
List<Object[]> results = query.list();
for (Object[] row : results) {
Category current = (Category) row[0];
int grandChildCategoryCount = ((Number) row[1]).intValue();
int itemCount = ((Number) row[2]).intValue();
ItemTreeNode treeNode =
new ItemTreeNode(current, grandChildCategoryCount, itemCount);