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
Spring-data findFirstBy throws IncorrectResultSizeDataAccessException?
提问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
Top
and First
keywords are available since spring-data-jpa-1.7.1.RELEASE
. See Spring Data JPA Changelogand Limiting query results
Top
和First
关键字从spring-data-jpa-1.7.1.RELEASE
. 查看Spring Data JPA Changelog和限制查询结果