This content has been marked as final.
Show 2 replies
-
1. Re: Catching NoResultException
blabno Sep 4, 2008 11:48 AM (in response to leeth)As to NonUniqueException ... it is thrown when transaction is being commited which does not happen right on
em.persist
orem.merge
but later (i.e. method end). If I want to catch such errors I putem.flush
right afterem.persist
and all exceptions, if any, are thrown there. -
2. Re: Catching NoResultException
leeth Sep 4, 2008 1:11 PM (in response to leeth)I don't understand that. Because I dont commit anything, I'm just quering. I have writen a small pseudo example below to illustratede the situation.
This pseudo example won't work because the dao doesn't throw any of the subtype of PersistenceExceptions but a EJBTransactionRolledbackExceptions.try{ MyEntity myEntity = dao.findActive(); handleActive(myEntity); }catch(NoResultException e){ handleNoActive(); }catch(NonUniqueResultException e){ log.fatal("more than one active"); throw new IllegalStateException("Internal error: more than one active record"); } .... public MyEntity findActive(){ String queryString = "from entity where endDate is not null"; Query query = em.createQuery(queryString); return query.getSingleResult(); }