-
Type:
Improvement
-
Status: Resolved
-
Priority:
Major
-
Resolution: Complete
-
Affects Version/s: 1.1.3.RELEASE
-
Fix Version/s: 1.1.4.RELEASE
-
Component/s: PERSISTENCE
-
Labels:None
Currently in order to get a single result, findXXX gets a resultList and gets the first element from the list. This requires the method to be transactional because if it was not transactional, results.size would throw an exception stating that the session manager has been closed.
Using query.getSingleResult() is more efficient because it does not require a transaction.
http://download.oracle.com/javaee/5/api/javax/persistence/Query.html#getSingleResult()
Instead of this being generated (currently):
@Transactional
public static User User.findUser(Long id) {
if (id == null) return null;
Query query = entityManager().createQuery("SELECT o FROM User o WHERE o.id = :id").setParameter("id", id);
User result = null;
List results = query.getResultList();
if (results.size() > 0)
return result;
}
This should be generated:
public static User User.findUser(Long id) {
if (id == null) return null;
Query query = entityManager().createQuery("SELECT o FROM User o WHERE o.id = :id").setParameter("id", id);
try
catch (NoResultException ne)
{ return null; }}
A NonUniqueResultException (- if more than one result) should propagate out (not be masked) because if such an error occurs, it would identify a lack in database integrity. As it is currently, such a lack of integrity would be hidden.