From: Igor A T. (JIRA) <no...@at...> - 2006-06-20 23:34:33
|
EntityManager.find and Query.getSingleResult must throw not RuntimeException ---------------------------------------------------------------------------- Key: EJB-200 URL: http://opensource.atlassian.com/projects/hibernate/browse/EJB-200 Project: Hibernate Entity Manager Type: Improvement Components: EntityManager Versions: 3.2.0.cr1 Reporter: Igor A Tarasov I think it will good improvement for writing code with EntityManager. As from javadoc of java.lang.RuntimeException citate: "<code>RuntimeException</code> is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. " Empty result of query is standard situation, but Query.getSingleResult() and EntityManager.find throws javax.persistence.NoResultException, which is subclass of RuntimeException. It will better, if it retun empty or null result, or ever throw subclass of normal (not Runtime) exception. It's hard to handle situation with empty result of query if it throw RuntimeException. MyEntity e = em.createQuery("....").getSingleResult(); if (e == null) { // do some work } else { // do another work } Using standard pattern from Hibernate documentation in this situation is very and very unconvenient. As shown below, we can fogot to catch NoResultException, because it derived from RuntimeException. EntityManager em = this.emf.createEntityManager(); EntityTransaction tx = null; try { tx = em.getTransaction(); // do work MyEntity e = null; // we can fogot to catch NoResultException because it RuntimeException try { e = em.find(MyEntity.class, id); } catch (NoResultException ex) { // not good } // commit tx.commit(); } catch (RuntimeException ex) { if (tx != null && tx.isActive()) tx. } finally { em.close(); } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |
From: Emmanuel B. (JIRA) <no...@at...> - 2006-06-21 01:43:32
|
[ http://opensource.atlassian.com/projects/hibernate/browse/EJB-200?page=all ] Emmanuel Bernard resolved EJB-200: ---------------------------------- Resolution: Rejected The javadoc can be found in the EJB 3.0 specification http://www.jcp.org/en/jsr/detail?id=220 The exception is part of the specification > EntityManager.find and Query.getSingleResult must throw not RuntimeException > ---------------------------------------------------------------------------- > > Key: EJB-200 > URL: http://opensource.atlassian.com/projects/hibernate/browse/EJB-200 > Project: Hibernate Entity Manager > Type: Improvement > Components: EntityManager > Versions: 3.2.0.cr1 > Reporter: Igor A Tarasov > > > I think it will good improvement for writing code with EntityManager. > As from javadoc of java.lang.RuntimeException citate: > "<code>RuntimeException</code> is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. " > Empty result of query is standard situation, but Query.getSingleResult() and EntityManager.find throws javax.persistence.NoResultException, which is subclass of RuntimeException. It will better, if it retun empty or null result, or ever throw subclass of normal (not Runtime) exception. It's hard to handle situation with empty result of query if it throw RuntimeException. > MyEntity e = em.createQuery("....").getSingleResult(); > if (e == null) { > // do some work > } else { > // do another work > } > Using standard pattern from Hibernate documentation in this situation is very and very unconvenient. As shown below, we can fogot to catch NoResultException, because it derived from RuntimeException. > EntityManager em = this.emf.createEntityManager(); > EntityTransaction tx = null; > try { > tx = em.getTransaction(); > // do work > MyEntity e = null; > // we can fogot to catch NoResultException because it RuntimeException > try { > e = em.find(MyEntity.class, id); > } catch (NoResultException ex) { > // not good > } > // commit > tx.commit(); > } catch (RuntimeException ex) { > if (tx != null && tx.isActive()) tx. > } finally { > em.close(); > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |