Sorting methods

Report a typo

Here is our Purchase class:

@Entity
public class Purchase {
    @Id
    @GeneratedValue
    private long id;
    private int transactionYear;
    private int orderId;
    private String storeName;
    private String company;
    private long dateOfPurchase;

    // constructor, getters, setters
}

Which of the following methods, declared at PurchaseRepository, correctly sort the received data?

public interface PurchaseRepository extends CrudRepository<Purchase, Long> {
 
    @Query("SELECT p FROM Purchase p WHERE p.storeName = :storeName")
    List<Purchase> findByStoreNameAndSort(String storeName, Sort sort);

    @Query(value = "SELECT * FROM purchases WHERE transaction_year >= :year", nativeQuery=true)
    List<Purchase> findByYearAndSort(int year, Sort sort);
    
    @Query(value = "SELECT * FROM purchases WHERE company = :company ORDER BY date_of_purchase ASC", 
    nativeQuery=true)
    List<Purchase> findByCompanyAndSortByDate(String company);

    @Query("SELECT p FROM Purchase p WHERE p.orderId = :sort")
    List<Purchase> findByOrderIdAndSort(Sort sort);

    @Query("SELECT p FROM Purchase p ORDER BY :sort")
    List<Purchase> findAllAndSort(Sort sort);
}
Select one or more options from the list
___

Create a free account to access the full topic