Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Repository method for first()

In Grails, I can say Foo.first(). In Spring, I have repositories extending PagingAndSortingRepository and I've created a service method called getFirst() that does the following:

public Foo getFirst() {
  return fooRepository.findAll(new PageRequest(0, 1, new Sort("ASC", "id"))).getContent().get(0);
}

Is this the best way or is there a convenience method I'm just missing?

like image 378
Gregg Avatar asked May 09 '16 16:05

Gregg


1 Answers

Create a query method on the repository interface like this:

Foo findFirstByOrderByIdAsc();

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result

like image 146
LucasP Avatar answered Nov 20 '22 21:11

LucasP