0 Replies Latest reply on Apr 12, 2009 8:56 PM by Walter White

    JPA, simplified access to named queries

    Walter White Novice

      Hi all,


      Would it be advantageous if Named Queries could be accessed more easily accessed in Seam?


      @NamedQuery(name = "HttpSession.findBySessionId", query = "FROM HttpSession session WHERE session.sessionId = :sessionId")
      



      public interface HttpSessionFinder
      {
          HttpSession findBySessionId(final String sessionId);
      }
      



      In a component where you would need access to this, Seam would automatically intercept these calls.  You would just declare an interface along with the named queries.


      @In
      HttpSessionFinder httpSessionFinder;
      
      public void doStuff(final String sessionId)
      {
          HttpSession httpSession = httpSessionFinder.findBySessionId(sessionId);
          .
          .
          .
      }
      



      There are other ways to handle it as well, but it would simplify having to write:


      entityManager.createNamedQuery(HttpSession.findBySessionId).setParameter(sessionId, sessionId).getSingleResult();


      or getResultList();


      Another alternative would be to extend the EntityQuery class allowing any number of arguments to be passed and the arguments would be mapped in the XML:


      <jpa:query name="HttpSession.findBySessionId" query="FROM HttpSession session WHERE session.sessionId = :sessionId">
        <parameter index="0" name="sessionId"/>
      </jpa:query>
      



      @In
      org.jboss.seam.persistence.Query findBySessionId;
      
      public void doStuff(final String sessionId)
      {
          HttpSession httpSession = findBySessionId.getSingleResult(sessionId);
      }
      
      



      Walter