2016-10-28 64 views
1

我正在使用Spring Data休息爲我的應用程序。如何返回列表<String>或在春季字符串數據休息

我在庫中使用了以下方法。

public interface TestRepository() extends BaseRepository<TestTerm, Integer>{ 
    @Query("select distinct tt.term from TestTerm") 
     List<String> findDistinctTerm(); 
} 

當我執行上述方法。我收到以下錯誤相關Spring Data Rest (SDR) bug? Persistent Entity Must not be null

PersistentEntity must not be null 

問我怎樣才能解決這個問題?

我必須爲此編寫單獨的實現嗎?如果有的話,任何人都可以爲我提供示例嗎

更新: -

我已經試過這樣

方法1: -

@Query("select DISTINCT term from ForbiddenTerm") 
List<TestTerm> findDistinctByTerm(); 

錯誤: -

{ 「病因」:空, 「消息「:」PersistentEntity不能爲空!「 }

方法2: -

List<TestTerm> findDistinctByTerm(); 

錯誤: -

... 154 common frames omitted 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'forbiddenTermRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE] 
    ... 175 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'forbiddenTermRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException 
+1

您不能(直接)使用SDR,因爲它只能返回'TestTerm'類型的註冊實體。您需要聲明使用此存儲庫的另一個控制器。 – kuhajeyan

+0

你不需要單獨的實現。順便說一下,你使用的是Spring Boot嗎? –

+0

嘗試'@Query(「從TestTerm tt選擇不同的tt.term」)或者:@Query(「從TestTerm tt選擇不同的tt」)並返回一個TestTerm實體列表,然後將所有項列表 –

回答

0

你需要改變你的接口的名稱是這樣的:

@Repository 
public interface TestTermRepository() extends BaseRepository<TestTerm, Integer>{ 
@Query("select distinct tt.term from TestTerm") 
    List<String> findDistinctTerm(); 
} 

您還需要添加@Repository註釋

+0

我試過你的上述方法。它不起作用。 – karan

+0

你有沒有更改界面的名稱? –

+0

是的。我做到了。請參閱我的更新問題 – karan