Optimized HQL query