Java SpringDataREST(SDR)错误?持久实体不能为null

Java SpringDataREST(SDR)错误?持久实体不能为null,java,spring,spring-data-jpa,spring-data-rest,Java,Spring,Spring Data Jpa,Spring Data Rest,目前我正在为SpringDataREST开发一个POC。试图从存储库中获取可用的JSONO 我有一个实体类(NewTask) 以及相应的存储库 @RepositoryRestResource @PreAuthorize("hasRole('ROLE_ADMIN')") public interface NewTaskRepository extends CrudRepository<NewTask, Serializable>{ @Quer

目前我正在为SpringDataREST开发一个POC。试图从存储库中获取可用的JSONO

我有一个实体类(NewTask)

以及相应的存储库

    @RepositoryRestResource
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public interface NewTaskRepository extends CrudRepository<NewTask, Serializable>{


        @Query("SELECT t.address FROM NewTask t where t.id = :id")
        String findByMyId(@Param("id") int id);

      }         
我得到以下错误: {“原因”:null,“消息”:“persistentenentity不能为null!”}

下面是我的存储库的外观:请阅读每个方法的注释

   //Gives- PersistentEntity must not be null!!!
    @Query("SELECT t.address FROM NewTask t where t.id = :id")
    String findByMyId(@Param("id") int id);


    //WORKS FINE
    @Query("SELECT t.id FROM NewTask t where t.id = :id")
    int findId(@Param("id") int id);


    //WORKS FINE
    @Query("SELECT t.id FROM NewTask t where t.id = :id")
    Integer findIdTwo(@Param("id") int id);

    //Gives- PersistentEntity must not be null!!!
    @Query("SELECT t.id FROM NewTask t")
    List<Integer> findIds();
//Gives-persistentenentity不能为null!!!
@查询(“从NewTask t中选择t.address,其中t.id=:id”)
字符串findByMyId(@Param(“id”)int-id);
//很好
@查询(“从NewTask t中选择t.id,其中t.id=:id”)
int findId(@Param(“id”)int id);
//很好
@查询(“从NewTask t中选择t.id,其中t.id=:id”)
整数findidtow2(@Param(“id”)int-id);
//Gives-PersistentEntity不能为null!!!
@查询(“从NewTask t中选择t.id”)
列出findIds();
我不确定退货类型有什么问题。我参考了下面的链接以获得一些解决方案:

并在我的存储库中添加了另外两个方法,但这些方法对我来说并不适用

    // returns in some weird format
    @Query("SELECT t.address FROM NewTask t where t.id = :id")
    PString findByMyId(@Param("id") int id);

    //Gives- PersistentEntity must not be null!!!
    @Query("SELECT t.address FROM NewTask t")
    List<PString> findAddress();
//以某种奇怪的格式返回
@查询(“从NewTask t中选择t.address,其中t.id=:id”)
PString findByMyId(@Param(“id”)int-id);
//Gives-PersistentEntity不能为null!!!
@查询(“从NewTask t中选择t.address”)
列出findAddress();

我有预感这是SDR中的一个bug,还是我遗漏了什么?

Spring Data REST只能返回存储库注册的数据。在您提到的另一个问题中,您会注意到他们专门为所需的类型创建了一个自定义存储库。这不是SDR中的错误,只是它的功能。它还能让它保持安静


因此,在您的情况下,SDR只能返回NewTask或NewTask集合。

在Spring Boot中,如果Spring主应用程序未能扫描域(POJO)类,则将抛出此错误,除非您将所有类放在同一个包中。然后在Spring主应用程序类的顶部添加组件扫描注释

@ComponentScan(basePackages = "com.aaa.bbbb.ccccc")

我使用的是SpringDataMongo,而不是JPA,但对我来说有效的是在Mongo“select”语句中包含\u class字段。Spring使用此字段将DB结果映射回实体/文档

还可以查看@TypeAlias,以便在域类之间来回移动时获得一致的类值


当我使用Springprojections预先预测DB结果时,我也遇到了同样的问题。后来,当我将结果转换为分页资源时,框架无法将投影接口标识为持久实体,因此无法找到为我进行转换所需的元数据。此用例的解决方案是一个自定义资源汇编程序。

重复问题。嗨,布兰登,你发布的答案在我的情况下不起作用。此外,这不是继承问题,没有基类,但错误消息是相同的。如果你有什么想法,可以帮我解决吗?或者你能推荐一个可以这样做的人吗?是的,我已经猜出了这一部分,但是正如你在上面的问题中看到的,为什么当我返回一个整数时它不会抛出相同的错误呢?我在返回一个实体的枚举属性时遇到了一个非常类似的问题:'@Query(“select d.status from Deal as d其中d.id=:id”)Deal.status findStatusById(@Param(“id”)长id);'我明白,SpringDataREST应该只返回资源,但为什么int可以呢?另一方面,如果我想将此查询从spring data rest中排除,这怎么可能呢?int是可以接受的,因为它被假定为资源的id(@id注释位于int字段上)。假设我想通过查询用户表中的name列返回列表。如何在SpringDataREST中实现这一点。
    // returns in some weird format
    @Query("SELECT t.address FROM NewTask t where t.id = :id")
    PString findByMyId(@Param("id") int id);

    //Gives- PersistentEntity must not be null!!!
    @Query("SELECT t.address FROM NewTask t")
    List<PString> findAddress();
@ComponentScan(basePackages = "com.aaa.bbbb.ccccc")