【发布时间】:2015-03-14 05:09:46
【问题描述】:
我们有一个名为 Location 的类的项目,并且已经成功实现了 jparepositories,定义了几种搜索方法。我们希望有一个额外的搜索方法,它会返回一组街道名称(从位置表中提取)而不是一组位置 - 这样我们就可以在用户输入街道时实现自动完成。首先我们尝试了@Query 注解:
@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 开头的街道),我得到一个空集 {} 返回。如果我查询确实存在的街道(street=Br,因为数据库中存在百老汇),我得到 p>
{"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 不能为空”的错误。实现中的代码用于返回硬编码结果,因此我们不会试图确定我们的 SQL 是否错误或我们的架构。我们在调试下运行它并验证确实返回了两个项目的列表。
问题似乎是从存储库返回列表以外的任何内容。这是对这种架构的限制吗?还是我们做错了什么,如果我们学会了秘密握手,一切都会变得很糟糕?
【问题讨论】:
标签: rest repository spring-data-jpa