java Spring-data findFirstBy 抛出 IncorrectResultSizeDataAccessException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27400711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:44:56  来源:igfitidea点击:

Spring-data findFirstBy throws IncorrectResultSizeDataAccessException?

javaspring

提问by membersound

I want to use sql select by method name in spring-data.

我想在spring-data中按方法名称使用sql select。

The select should be ordered 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 exactly used the findFirst()method for this reason to just get the top result if more than one is found..

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

回答by Tkachuk_Evgen

Topand Firstkeywords are available since spring-data-jpa-1.7.1.RELEASE. See Spring Data JPA Changelogand Limiting query results

TopFirst关键字从spring-data-jpa-1.7.1.RELEASE. 查看Spring Data JPA Changelog限制查询结果