Skip to content

Commit

Permalink
Adds a function for ID-based database searching, and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Mar 14, 2024
1 parent 724a0bf commit 29a0df0
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import javax.persistence.PersistenceException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
Expand All @@ -38,6 +40,7 @@
* Base class for DAOs.
*/
public abstract class BaseDAO<T extends BaseBean> implements Serializable {
private final Logger logger = LogManager.getLogger(getClass());

private static final Object lockObject = new Object();

Expand Down Expand Up @@ -185,6 +188,7 @@ public List<T> getByQuery(String query, Map<String, Object> parameters, int firs
addParameters(q, parameters);
return q.list();
} catch (SQLGrammarException e) {
logger.catching(e);
return Collections.emptyList();
}
}
Expand Down Expand Up @@ -284,6 +288,44 @@ static void removeObject(Class<?> cls, Integer objectId) throws DAOException {
}
}

/**
* Returns the list of object IDs returned by a query.
*
* <p>
* To display lists that are many pages long, based on conditional database
* queries, the IDs of the objects to be displayed are first determined in
* the database without deserializing the objects. The required section of
* objects can then be retrieved later using the list. However, basic
* information required for list pagination, such as the total length of the
* list, and a fixed list position for each object, is already given.
*
* <p>
* Not loading all potential display candidates at the time of request
* speeds up the display significantly, or even makes it usable at all for
* larger data sets (considering several 100k records in a table), thus
* enabling a smooth user experience.
*
* @param query
* HQL query kind of "SELECT id FROM ClassName WHERE …"
* @param parameters
* query parameters. May be {@code null}.
* @return list of object IDs
*/
public List<? extends Number> getIdsByQuery(String query, Map<String, Object> parameters) {
String objectClass = getClass().getName().replaceFirst("DAO$", "");
if (!(query.substring(0, 15).equalsIgnoreCase("SELECT id FROM ")
&& query.substring(15, 16 + objectClass.length()).equals(objectClass.concat(" ")))) {
throw new IllegalArgumentException("query must start with 'SELECT id FROM " + objectClass
+ "', but was: " + query);
}
try (Session session = HibernateUtil.getSession()) {
Query<?> questioner = session.createQuery(query);
addParameters(questioner, parameters);
List<?> answer = questioner.list();
return answer.stream().map(Number.class::cast).collect(Collectors.toList());
}
}

/**
* Initialize child list of objects for given base bean.
*
Expand Down

0 comments on commit 29a0df0

Please sign in to comment.