我们有一个名为Location的类的项目,并成功实现了jparepositories,定义了几种搜索方法。我们希望有一个额外的搜索方法,可以返回一组街道名(从位置表中提取)而不是一组位置 - 所以我们可以在用户输入街道时实现自动填写。首先,我们尝试了@Query注释:如何在jparepository中创建自定义查询,但返回实体以外的对象?
@RepositoryRestResource(collectionResourceRel = "locations", path = "locations")
public interface LocationRepository extends JpaRepository<Location, Integer>, LocationRepositoryCustom {
List<Location> findByStreetNameStartingWithIgnoreCase(@Param("street") String streetName);
@Modifying
@Query("select x.streetName from Location x where x.streetName like :street%")
List<String> findStreetNameStartingWith(@Param("street") String streetName);
}
如果我查询的程序并不存在一个街道(街道没有开始,X,例如),我得到了一个空集{}返回。如果我问的是确实存在一个街道(街道= BR,因为百老汇在数据库中存在),我得到
{"cause":null,"message":"PersistentEntity must not be null!"}
然后我们试图执行它作为一个自定义查询,使用:
public interface LocationRepositoryCustom {
@Query("select x.streetName from Location x where x.streetName like :streetName")
public List<String> collectStreetNames(@Param("streetName") String streetName);
}
class LocationRepositoryImpl implements LocationRepositoryCustom {
@PersistenceContext
private EntityManager em;
@Override
public List<String> collectStreetNames(String streetName) {
List<String> retList = new ArrayList<String>();
retList.add("start");
retList.add("end");
return retList;
}
}
这也给我们“PersistentEntity must not null”的错误。实现中的代码被用来返回一个硬编码的结果,所以我们不想弄清楚我们的SQL是错误的还是我们的体系结构。我们在调试下运行它,并确认返回了两个项目的列表。
该问题似乎是从存储库中返回除列表以外的任何内容。这是对这个架构的限制吗?还是有什么我们做错了,而且如果我们学会了秘密的握手,一切都将是疯狂的?
这种方法对我来说效果不好,你找到了解决方案吗?如果是的话,你可以在这里回答:http://stackoverflow.com/questions/33538426/spring-data-rest-sdr-bug-persistent-entity-must-not-be-null – 2015-11-05 07:10:46