Spring-data findFirstBy throwsestResultSizeDataAccessException?

2022-09-03 05:55:49

我想在弹簧数据中使用sql按方法名称选择。

选择应按价格排序。

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

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

结果:

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

为什么?出于这个原因,我完全使用了这种方法,以便在找到多个结果时获得最高结果。findFirst()


答案 1

Top和关键字自 .请参阅 Spring Data JPA 更改日志限制查询结果Firstspring-data-jpa-1.7.1.RELEASE


答案 2

推荐