Spring Data Rest (SDR) 错误?持久实体不能为空 [英] Spring Data Rest (SDR ) bug? Persistent Entity Must not be null

查看:20
本文介绍了Spring Data Rest (SDR) 错误?持久实体不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I am working on a POC for Spring Data Rest. Trying to get workable JSONout of a repository.

我有一个实体类(NewTask)

I have an Entity Class (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 不能为空!"}

I get the following error: {"cause":null,"message":"PersistentEntity must not be null!"}

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

Now here is how my repository looks: Please read the comments on each method

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

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

I am not sure what are the issues with return types.I referred the link below for some solution:

如何在 jparepository 中创建自定义查询但返回实体以外的对象?

并向我的 Repository 添加了另外 2 个方法,这些方法对我不起作用

and added 2 more methods to my Repository, which don't work for me

    // 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 中的错​​误,还是我遗漏了什么?

I have a hunch this is a bug in SDR, or am I missing something?

推荐答案

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

Spring Data REST can only return data for which the repository is registered for. In the other question that you reference, you'll notice that they created a custom repository specifically for the type that they needed. This isn't a bug in SDR, it's just how it functions. It also keeps it RESTful.

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

So in your case, SDR can only return NewTask or collections of NewTask.

这篇关于Spring Data Rest (SDR) 错误?持久实体不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆