【问题标题】:Spring Data Rest (SDR ) bug? Persistent Entity Must not be nullSpring Data Rest (SDR) 错误?持久实体不能为空
【发布时间】:2016-02-05 22:13:00
【问题描述】:

目前我正在为 Spring Data Rest 开发 POC。试图从存储库中获取可行的 JSONout。

我有一个实体类(NewTask)

@Entity
@Table(name="newtable")
public class NewTask {

    @Id
    @Column(name="newid")
    private int id;


    @Column(name="newage")
    private int age;

    @Column(name="newaddress")
    private String address;



    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

以及相应的存储库..

    @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);

      }         

每当我击球时

http://localhost:8080/POCDB/newTasks/search/findByMyId?id=1

我收到以下错误: {"cause":null,"message":"PersistentEntity 不能为空!"}

现在这是我的存储库的外观:请阅读每个方法的 cmets

   //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();

我不确定返回类型有什么问题。我参考了以下链接以获得一些解决方案:

How does one create a custom query in jparepository but return an object other than the entity?

并在我的存储库中添加了另外 2 个方法,它们对我不起作用

    // 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();

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

【问题讨论】:

  • 嗨,布兰登,您发布的答案在我的情况下不起作用。另外,这不是继承问题,没有基类,但错误信息是一样的。如果您有任何想法,您能帮忙解决一下吗?或者你能推荐一个可以的人吗?

标签: java spring spring-data-jpa spring-data-rest


【解决方案1】:

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

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

【讨论】:

  • 是的,我想出了那部分,但正如您在上面的问题中看到的,为什么当我返回一个整数时它不会抛出同样的错误?
  • 我在返回实体的枚举属性时遇到了一个非常相似的问题:' @Query("select d.status from Deal as d where d.id = :id") Deal.Status findStatusById( @Param("id") 长 id);'我知道, spring-data-rest 不应该返回资源以外的任何东西,但是为什么 int 可以呢?另一方面,如果我想排除这个查询暴露给 spring-data-rest,这怎么可能?
  • 一个 int 是可以接受的,因为它被假定为一个资源的 id(一个 int 字段上的@Id 注释)。
  • 假设我想通过从用户表中查询名称列来返回一个 List。我如何在 Spring Data REST 中这样做。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我使用 Spring Data Mongo 而不是 JPA,但对我有用的是在 Mongo“select”语句中包含 _class 字段。 Spring 使用该字段将 DB 结果映射回实体/文档。

    还请查看 @TypeAlias,以便您在调整域类时拥有一致的 _class 值。

    当我使用 Spring projections 预先预测数据库结果时,我也遇到了同样的问题。后来,当我将结果转换为分页资源时,框架无法将投影接口识别为持久实体,因此无法找到为我进行转换所需的必要元数据。此用例的解决方案是自定义资源组装器。

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多