/** * @author SacrosanctBlood * * * <p> * This utility class is used to execute an hibernate query on a single entity class, with any number of conditions * (equals conditions only, but can be extended)on the attributes of the class. It also fetches the Objects given in the * list which would otherwise be fetched lazily. * </p> */public class HibernateQueryUtil { /** * This method returns the result of the find query on the given input class. * * @param session * The hibernate session in which the query will be executed. This can not be null. * @param clazz * The entity class which is mapped to a table in database, on which the query is executed. This can not * be null. * @param conditions * A map of attributeName = attributeValue for the attributes in the class. These represents the * conditions for the fetch query. This can be null. * @param fetchList * A list of attributes in the class representing a foreign relationship that needs to be fetched * eagerly. This can be null. * @return List A list of the resultant Objects of the type clazz which have been eagerly initialized according to * the fetchList. The List can be empty dependent on the query executed. * @throws IllegalArgumentException * Thrown when * <li>Clazz is not valid</li> * <li>Attributes are not present in clazz</li> * <li>Attributes in fetchList is not valid.</li> */ public static List find(Session session, Class clazz, Map<String, Object> conditions, List<String> fetchList) throws IllegalArgumentException { Set<String> attributes = null; attributes = initializeAttributes(conditions); String entityName = clazz.getSimpleName(); List result = null; if (entityName != null) { Criteria criteria = session.createCriteria(clazz); if (conditions != null) { checkIfAttributesExistInClass(clazz, attributes); addCriterions(criteria, conditions, attributes); } result = executeQuery(criteria); if (fetchList != null && result != null) { checkIfAttributesExistInClass(clazz, fetchList); List<String> methodsToInvoke = convertFetchListToMethodToInvokeList(fetchList); checkIfMethodsExistInClass(clazz, methodsToInvoke); initializeFetchList(clazz, methodsToInvoke, result); } } else { throw new IllegalArgumentException("Class " + clazz.getName() + " is not valid"); }