protected Optional<T> doInTransaction(EntityManager entityManager, String queryString
) {
Query query = entityManager.createQuery(queryString);
if (argument.length % 2 != 0) {
String message = "The arguments for the query should be a list of <String, Object> pairs";
throw new AssetManagerRuntimeException(message);
}
for (int index = 0; index < argument.length; index = index + 2) {
String label = (String) argument[index];
Object value = argument[index + 1];
query.setParameter(label, value);
}
try {
T result = (T) query.getSingleResult();
return Optional.of(result);
} catch (NoResultException e) {
logger.info("No result found searching a unique element", e);
return Optional.absent();
} catch(NonUniqueResultException nonUniqueResultException){
logger.error("Non unique result found searching a unique element", nonUniqueResultException);
throw new AssetManagerRuntimeException("Returned more that one element in the query", nonUniqueResultException);
}
}
};
}