【发布时间】:2016-10-28 09:36:23
【问题描述】:
我正在为我的应用程序使用 Spring Data rest。
我在我的存储库中使用以下方法。
public interface TestRepository() extends BaseRepository<TestTerm, Integer>{
@Query("select distinct tt.term from TestTerm")
List<String> findDistinctTerm();
}
当我执行上述方法时。我收到以下错误
PersistentEntity must not be null
与Spring Data Rest (SDR ) bug? Persistent Entity Must not be null相关的问题
我该如何解决这个问题?
我必须为此编写单独的实现吗?如果是这样,任何人都可以为我举个例子吗?
更新:-
我试过这样
方法一:-
@Query("select DISTINCT term from ForbiddenTerm")
List<TestTerm> findDistinctByTerm();
错误:-
{ “原因”:空, "message": "PersistentEntity 不能为空!" }
方法二:-
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
【问题讨论】:
-
您不能(直接)使用 SDR,因为它只能返回“TestTerm”类型的注册实体。您需要声明另一个使用此存储库的控制器。
-
您不需要单独的实现。顺便问一下,你用的是 Spring Boot 吗?
-
尝试
@Query("select distinct tt.term from TestTerm tt")或执行:@Query("select distinct tt from TestTerm tt")并返回TestTerm实体列表,然后将所有术语放入列表中
标签: java spring rest spring-mvc spring-data-rest