5

I'm having an entity embedded into another as shown below. The repository is created on the DepEntity. I am trying retrieve the DepEntity objects sorted on totalExp column in ExpEntity. I m making a GET call using Pageable and getting the error:

2018-12-18 05:17:58.172 WARN 7 --- [http-nio-8000-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.data.mapping.PropertyReferenceException: No property totalExp found for type DepEntity!]

I tried with totalExp, exp.totalExp and exp_totalExp and none of them worked. The spring data jpa version is 2.1.1

@Entity
public class DepEntity {
    @Embedded
    private ExpEntity exp;
}

@Embeddable
public class ExpEntity {
    @Column(name = "exp_total")
    private BigDecimal totalExp;
}

I expect the results in sorted order on the nested property totalExp. Is there a way to achieve this?

Bucket
  • 7,415
  • 9
  • 35
  • 45
Santosh M K
  • 87
  • 2
  • 8

1 Answers1

3

Below should work:

Sort sort = Sort.by("exp.totalExp").descending();
PageRequest pageRequest = PageRequest.of(0, 10, sort);
Page<DepEntity> depEntities  = depEntityRepository.findAll(pageRequest);
Logan Farci
  • 5
  • 1
  • 2
Aditya Narayan Dixit
  • 2,105
  • 11
  • 23