Spring Data Rest(SDR)错误?持久实体不能为空 [英] Spring Data Rest (SDR ) bug? Persistent Entity Must not be null
问题描述
目前我正致力于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屋!