Uploaded image for project: 'Spring Roo'
  1. Spring Roo
  2. ROO-2338

findXXX(id) should user query.getSingleResult() instead of query.getResultList()

    XMLWordPrintable

    Details

    • 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

      Description

      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)

      { result = (User) results.get(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

      { return (Image) query.getSingleResult(); }

      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.

        Attachments

          Activity

            People

            Assignee:
            stewarta Alan Stewart
            Reporter:
            sdmurphy Scott Murphy
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: