Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-967

Default accessors on projection interfaces must not be considered input properties

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.13 RC1 (Ingalls), 2.0 M1 (Kay), 1.12.6 (Hopper SR6)
    • Component/s: Core
    • Labels:
      None
    • Environment:
      Spring 4.3.5, Hibernate 5.1.3
    • Sprint:
      Ingalls GA

      Description

      Recently, I tried to transform my projection interfaces into closed projections to optimize database queries. Before, I used @Value annotations aka. open projections in various places:

      public interface ContactPersonProjection {
      
          @Value("#{target.contactPerson?.fullName}")
          String getContactPersonFullName();
      
          @Value("#{target.contactPerson?.login}")
          String getContactPersonLogin();
      }
      

      I thought that Java 8's default method implementations might be a good way to workaround this limitation. My new projection looks like this:

      public interface ContactPersonProjection {
      
          @JsonIgnore
          Person getContactPerson();
      
          default String getContactPersonFullName() {
              return getContactPerson() == null ? null : getContactPerson().getFullName();
          }
      
          default String getContactPersonLogin() {
              return getContactPerson() == null ? null : getContactPerson().getLogin();
          }
      }
      

      But it seems the query lookup strategy tries to create queries for the default methods, too.

      java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [fullName] on this ManagedType [com.example.models.Person]
      	at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:128)
      	at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:113)
      	at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:566)
      	at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:573)
      	at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:161)
      	at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:137)
      	at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:49)
      	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
      	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
      	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:118)
      	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:69)
      	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
      

      It would be great to have this working out of the box.

      PS: Default methods work like expected on open projections (e.g. when there's already one @Value method present).

        Attachments

          Activity

            People

            Assignee:
            olivergierke Oliver Drotbohm
            Reporter:
            koraktor Sebastian Staudt
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: