Reputation: 41
I want a JPA method query that does the equivalent of select * from my_table order by my_col
. I can use the findAll()
method to select * from my_table
to get all columns and rows. I can declare and use findAllBySomeColOrderByMyCol(someCol)
to get some rows and all columns and sort on my_col
. What I can't find is findAllOrderByMyCol()
to get all rows and columns and sort on one of the columns.
Does this really not exist? If not, what is the rationale? All are select *.
Upvotes: 1
Views: 3524
Reputation: 1
Try to do like me below, it should work:
import com.khaled.caterate.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userRepository")
public interface UserRepository extends JpaRepository<User, Integer> {
List<User> findAllByFirstNameOrderByAverageMark(String firstName);
}
Upvotes: 0
Reputation: 41
My coworker helped me figure this out. The method is findAllByOrderByMyCol()
. I promise I read https://docs.spring.io/spring-data/jpa/docs/1.8.x/reference/html/#jpa.query-methods, which seem like the logical docs. The naming convention (using that first 'By') just didn't seem intuitive to me, and nothing in the docs really pointed it out.
Hope this helps someone else.
Upvotes: 1