2014-12-10 17 views
10

Voglio usare sql selezionare per nome del metodo in spring-data.Spring-data findFirstBy genera IncorrectResultSizeDataAccessException?

La selezione deve essere ordinata per prezzo.

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

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

Risultato:

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

Perché? Ho usato esattamente il metodo findFirst() per questo motivo per ottenere solo il risultato superiore se più di uno si trova ..

+0

quale versione di springdata si sta utilizzando? –

+0

@Tkachuk_Evgen spring-boot 1.9.1.RELEASE, quindi spring-data-jpa-1.6.4.RELEASE – membersound

+1

top e first sono disponibili da 1.7.1.RELEASE [Spring Changelog JPA Data] (http: //docs.spring .io/autorepo/docs/spring-data-jpa/1.7.1.RELEASE/changelog.txt) –

risposta