jpa-spec
  1. jpa-spec
  2. JPA_SPEC-26

Add fetch profile support for queries

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Labels:
      None

      Description

      The spec currently does not provide a possibillity to add additional join fetches for an existing query.
      Queries that have only one driving table/entity like,

      Unable to find source-code formatter for language: jpql. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
      FROM Entity e JOIN FETCH e.relation
      

      could also be written as,

      Unable to find source-code formatter for language: jpql. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
      FROM Entity e
      

      and be transformed by providing the property paths at runtime.
      The property path for this example would be relation.

      I suggest to add the following method:

      Query.java
      public interface Query{
        // currently available methods...
      
        public Query fetch(String... propertyPath);
      }
      

      A basic query could then be transformed at runtime to fit the needs of the requester of objects.

      A basic service method that returns a list of objects could be extended to provide the option of passing the property paths as parameters like:

      public class BasicService{
        @PersistenceContext
        private EntityManager em;
      
        public List<Entity> getEntities(Integer forUserId){
          // default fetching
          // expected query is "FROM Entity e JOIN FETCH e.relation WHERE e.userId = :userId"
          return getEntities(forUserId, "relation");
        }
      
        public List<Entity> getEntities(Integer forUserId, String... propertyPaths){
          // using the given property paths
          return em.createQuery("FROM Entity e WHERE e.userId = :userId")
                   .setParameter("userId", forUserId)
                   .fetch(propertyPaths)
                   .getResultList();
        }
      }
      

      If you have two views for example, that would like to use that service, but one view needs an additional relation to be fetched, you would have to copy the method with all of the code and fetch the needed relation within the copied method. The suggested approach of using property paths for fetching relations would reduce the total number of queries and methods needed in an application.

      Additionally a more type safe approach could be provided by using the static metamodel, but I did not thought through that one. I am not sure if deep paths, like relation.anotherRelation.yetAnotherRelation could be represented when using static metamodel, but at least with strings it is possible.

      Hibernate already has a simmilar feature, but it is not as flexible as this approach would be(also see http://docs.jboss.org/hibernate/orm/3.5/reference/en/html/performance.html#performance-fetching-profiles).

      1. FetchProfile.java
        1 kB
        c.beikov
      2. FetchUtil.java
        8 kB
        c.beikov
      3. FetchUtilTest.java
        6 kB
        c.beikov

        Activity

        c.beikov created issue -
        Hide
        c.beikov added a comment -

        I added a really simple strategy that places an identifier in a base query and replaces it by the actual fetch code. A test for that approach is also provided.

        Show
        c.beikov added a comment - I added a really simple strategy that places an identifier in a base query and replaces it by the actual fetch code. A test for that approach is also provided.
        c.beikov made changes -
        Field Original Value New Value
        Attachment FetchProfile.java [ 50184 ]
        Attachment FetchUtil.java [ 50185 ]
        Attachment FetchUtilTest.java [ 50186 ]
        Hide
        ldemichiel added a comment -

        This is now addressed by the entity group functionality. Entity groups can specify fetch plans for queries.

        Show
        ldemichiel added a comment - This is now addressed by the entity group functionality. Entity groups can specify fetch plans for queries.
        ldemichiel made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            c.beikov
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: