Spring Data: Nested Property Order

I use JpaRepositoryboth JpaSpecificationExecutorfrom Spring Data and I had a problem sorting a methodfindAll(specification, pageable, sort)

I want to sort the result of a specification using an attached property from the main repo class. This is my case:

main class

class Foo {
    //other properties
    @OneToMany(mappedBy="foo")
    private Set<Bar> bars;
}

ordering class

class Bar {
    @ManyToOne
    @JoinColumn(name="fooId")
    private Foo foo;

    //I WANT TO SORT BY THIS FIELD
    @Column
    private Date date;
}

and this is my repo

interface FooRepo extends JpaRepository<Foo , Long>, 
        JpaSpecificationExecutor<Foo>{
    //just jparepo methods
}

this is how i try to arrange this result

void anymethod(){
    Sort sort = new Sort(Bar_.date.getName());
    PageRequest pr = new PageRequest(anyPage, anyMaxResultsNum, sort);
    repository.findAll(anySpecification, pr);

}

and when I run this, I get "PropertyReferenceException: no property date for type Foo!"

How can i do this?

+4
source share
2 answers

You can use annotation @javax.persistence.OrderBy:

@OneToMany(mappedBy="foo")
@OrderBy("date")
private Set<Bar> bars;
+5
source

date Bar, Foo. a BarRepo findAll() BarRepo, . , Foo Bar, findAll(), Foo.

@Query FooRepo SQL- .

0

All Articles