Do I always need to enclose the saveOrUpdate
or delete
on Session
in DAOs with try-catch block like this bellow?
public void save(Inventory object) {
try {
factory.getCurrentSession().saveOrUpdate(object);
} catch (Exception e) {
_logger.error("Cannot save or update object " + object, e);
}
}
Generally it depends on whether you want to handle specific exception in your DAO or not. But note, that in your specific example, session may not be flushed and as a result you will not get any interesting exceptions (like constraint violation) anyway. So I'd say it makes less sense to catch them than to allow them to propagate. But there is no firm rule.