【发布时间】: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