2016-11-18 23 views
0

我喜歡在SortimentRespository中有一個findAll方法,該方法允許排序和分頁,並且可以在當前頁面傳遞一個參數,並將頁面大小和我想要排序的列的名稱不區分大小寫。如何使用Spring Data Rest對數據進行大小寫不敏感?

請求應類似於:

http://localhost:8081/x/rest/sortiments?size=20&page=1

http://localhost:8081/x/rest/sortiments?size=20&sort=name,desc&page=1

我想在我的倉庫下面的方法

@Transactional 
    public interface SortimentRepository extends JpaRepository<Sortiment,   RootKey>, SortimentRepositoryCustom { 
     List<Sortiment> findAll();    
     // Query 1 
     Page<Sortiment> findAllIgnoreCase(Pageable pageable); 

     // Query 2 
     Page<Sortiment> findAllByOrderByNameAscIgnoreCase(Pageable pageable); 

     // Query 3 
     @Query("select s from Sortiment s order by LOWER(s.name)") 
     Page<Sortiment> findAllOrderByNameIgnoreCase(Pageable p); 
    } 

結果

查詢1: 運行時出錯:org.springframework.beans.factory.BeanCreationException:使用名稱'sortimentRepository'創建bean時出錯:init方法的調用失敗;嵌套異常是org.springframework.data.mapping.PropertyReferenceException:找不到類型Sortiment的屬性!

查詢2: org.springframework.beans.factory.BeanCreationException:創建名爲'sortimentRepository'的bean時出錯:init方法的調用失敗;嵌套異常是org.springframework.data.mapping.PropertyReferenceException:沒有找到類型Sortiment的屬性ignoreCase!

查詢3: 我甚至看不到下鏈接的搜索在HAL瀏覽器這種方法

有誰有一個想法,我怎麼可能實現insenstitive排序的情況下?這將是和是非常好的,如果我有什麼globale因爲幾個庫將需要的findAll不區分大小寫的方法...

+0

重複http://stackoverflow.com/questions/15777638/case-insensitive-sort-using-spring-data – sura2k

回答

0

首先,確保Sortiment豆標註有@Entity

春季數據遵循一些規則存儲庫方法名稱(在下面的鏈接中給出),你需要改變你的查詢方法,如下圖所示:

在服務層:

Pageable pageable = new PageRequest(pageNumber,pageSize,Sort.Direction.ASC,name); 

庫類:

@Transactional 
public interface SortimentRepository extends JpaRepository<Sortiment,RootKey>, 
    SortimentRepositoryCustom { 
     Page<Sortiment> findAll(Pageable pageable); 
    } 

您可以參考here的例子。

+0

我見過這個,但不同的是,我需要所有的數據,而不是一個特定的與一個給定的名字。 – Sarah

+0

我需要在無序情況下排序的所有排序。該非標準排序是這樣的: 機管局 了Bb AA BB ,我需要在這樣的秩序中的數據: 機管局 AA 了Bb BB 因爲這個要求,我需要所有日期的情況下insenstitive有序。 – Sarah

+0

請參考上面的答案 – developer