Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-data findFirstBy throws IncorrectResultSizeDataAccessException?

Tags:

java

spring

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

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..

like image 212
membersound Avatar asked Dec 10 '14 12:12

membersound


People also ask

Does JpaRepository extends PagingAndSortingRepository?

Let's start with the JpaRepository – which extends PagingAndSortingRepository and, in turn, the CrudRepository. Each of these defines its own functionality: CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records.

What is @query annotation in spring boot?

The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file. It's a good approach to place a query definition just above the method inside the repository rather than inside our domain model as named queries.

What is correct about Spring Data JP A?

Spring Data JPA is an add-onIt provides a framework that works with JPA and provides a complete abstraction over the Data Access Layer. Spring Data JPA brings in the concept of JPA Repositories, a set of Interfaces that defines query methods. The Repository and Entity Bean represent the DAO layer in the application.

What does @table do in spring?

The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalog, and its schema, and enforce unique constraints on columns in the table.


1 Answers

Top and First keywords are available since spring-data-jpa-1.7.1.RELEASE. See Spring Data JPA Changelog and Limiting query results

like image 66
Tkachuk_Evgen Avatar answered Sep 19 '22 06:09

Tkachuk_Evgen