我們有一個名爲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