i have a class called LoadDaoHbm.java in which my method is
@Override
@Transactional(propagation = Propagation.MANDATORY)
public int getPackageCount(Load load) {
Session s = getSessionFactory().getCurrentSession();
Query q = s.createQuery("select count(distinct p.trackingNumber) from BasePiece p where p.load = :load");
q.setEntity("load", load);
Long count = (Long) q.uniqueResult();
return count.intValue();
}
in my controller I am calling this method to get the count
int pkgCount = loadDao.getPackageCount(load);
when I get to this point in my controller, it goes to the method and gets the data. but, sometimes it might not get a unique result. When there are more results it gives me an exception. How can catch this exception and show as an error?
Suggestion is appreciated! Thanks in advance!
One way is in your controller do
Be careful to catch ONLY the exception thrown for the unique constraint violation. Else you might swallow other errors.
If this is not an error condition in your app, you might want to consider writing another DAO method to determine if unique condition is valid, and not use exceptions. Exceptions are for exceptional scenarios.