目前我正在为 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/