I have an entity which is expected to either be null or for there to be a single result. Currently I'm handling this like so:
Entity entity = null;
try {
entity = query.getSingleResult();
} catch (NoResultException ignore) {
return null;
}
return entity;
But is this the "best" way of doing this? I'm not a fan of ignoring exceptions but equally not a big fan of the only alternative I can think of:
List<Entity> entities = null;
entities = query.getResultList();
if (entities.size() > 0) {
return entities.get(0);
}
return null;