java - Spring Data Rest (SDR) 错误?持久实体不能为空

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

目前我正在为 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 不能为空!"}

现在这是我的存储库的外观:请阅读每种方法的评论
   //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 中的一个错误,还是我遗漏了什么?

最佳答案

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

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

关于java - Spring Data Rest (SDR) 错误?持久实体不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538426/

相关文章:

java - 无法实例化类型 Lock

spring - 将 JSON 反序列化器用于批处理作业执行上下文

java - 如何通过 REST API 关联两个实体

java - 测试 SpringBoot JPA 时出现 IncompatibleClassChangeError

java - JPA手动加载集合

java - 我忘记停止 GUI 程序并再次运行它。现在我无法在不杀死 eclipse 的情况下关闭第一个程序

java - 无法从 GPS 检查返回 boolean 值

java - 关于 Java 文件加密性能的建议

model-view-controller - 如何通过 addChild 便捷方法将子集合元素绑定(bind)到父命令类(Spring MVC)

spring-data - 使用 spring-data-rest 向集合添加资源