0

Our frontend is designed to send Pageable object with Spring's Order which contains attribute name.

Sample Entity:

@Entity
@Table
public class Foo {

   private String userName;

   @Embedded
   private Bar bar;
}

@Embeddable
public class Bar {
   private String value;
}

We receive pageable with order in repository and parse JPA's Order from that pageable object like this:

pageable.getSort().get().forEach(springOrder -> 
        jpaOrders.add(getBuilder().asc(root.get(springOrder .getProperty())))
        );

We need do it this way because we are building queries via criteria api.

When we receive in repository attribute of Foo, everything is fine, e.g. "userName". But when we receive attribute of embeddable entity, e.g. "bar.value" we got exception

Unable to locate Attribute with the the given name [bar.value] on this ManagedType

Can you tell me how to create JPA's Order object from attribute which define attribute of embeddable entities? Thank you in advice

Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174
  • Why are you having to 'parse' the Pageable? You should be able to pass this straight to the repository. Have you implemented PagingAndSortingRepository https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html – Alan Hay Feb 19 '19 at 14:26
  • @AlanHay I added reason also to question. We have advanced queries so we are using criteria api for them. – Denis Stephanov Feb 19 '19 at 14:41
  • Consider specification pattern (using Criteria API or with QuerDsl as a (IMO, simpler) alternative). See JpaSpecificationExecutor and QueryDslPredictaeExecutor which have methods to take the specificatin/predicate and the Pageable. https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ – Alan Hay Feb 19 '19 at 14:56
  • @AlanHay we switch from querydsl because of some issues. But thanks for advice. – Denis Stephanov Feb 19 '19 at 15:08

2 Answers2

0

For these property paths you will have to create joins and apply the Order using the proper join alias and the last part of the property path.

For more details, I'd recommend looking into the code of Spring Data JPA. Find all references to the Order class and start reading some code.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
-1

Have you tried using org.springframework.data.jpa.repository.query.QueryUtils#toOrders ?

halfelf
  • 9,737
  • 13
  • 54
  • 63
Conrad
  • 536
  • 5
  • 13