Java – spring data findfirstby throws correctresultsizedataaccessexception?

I want to use SQL SELECT. XML with method name in spring data

The selection should be sorted by price

@Entity
public class Product {
    int name;
    BigDecimal price;
}

interface ProductRepository extends CrudRepository<Product,Long> {
    Product findFirstByNameOrderByPriceAsc(String name);
}

result:

org.springframework.dao.IncorrectResultSizeDataAccessException: result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException: result returns more than one elements

Why? I happen to use the findfirst () method. As long as I find multiple results, I can get the highest result

Solution

From spring-data-jpa-1.7 1. From release, you can use the top and first keywords See spring data JPA changelog and limiting query results

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>