2015-01-15 354 views
4

我們有一個名爲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是錯誤的還是我們的體系結構。我們在調試下運行它,並確認返回了兩個項目的列表。

該問題似乎是從存儲庫中返回除列表以外的任何內容。這是對這個架構的限制嗎?還是有什麼我們做錯了,而且如果我們學會了祕密的握手,一切都將是瘋狂的?

回答

0

我錯過的線索是'PersistentEntity must not null'。存儲庫框架想要返回一個註冊實體的列表 - 不是任何舊的POJO或原語。解決的辦法是定義的查詢可以返回一個實體:

@Entity 
@Table(name="PString") 
public class PString { 

    @Column(name="Name", length=40) 
    @Id 
    private String value; 

    public PString() { } 
    public PString(String name) { 
    this.value = name; 
    } 

    public String getValue() { 
    return value; 
    } 
    public void setValue(String value) { 
    this.value = value; 
    } 
} 

伴隨着這一點,需要一個標準PStringRepository:

@RepositoryRestResource(collectionResourceRel = "strings", path = "strings") 
public interface PStringRepository extends JpaRepository<PString, String> { 
} 

然後,在LocationRepositoryCustom我的自定義函數變爲:

@Override 
public List<PString> collectStreetNames(String streetName) 
{ 
    Query query = em.createNativeQuery("select distinct streetName from Location where streetName like ?"); 
    query.setParameter(1, streetName + "%"); 
    List<PString> returned = new ArrayList<PString>(); 

    @SuppressWarnings("unchecked") 
    List<String> list = query.getResultList(); 

    for (String string : list) 
    { 
     returned.add(new PString(string)); 
    } 

    return returned; 
} 

現在返回一個StreetNames列表。 (它們被格式化爲字符串項目的hrefs,因此所有空格都被替換爲%20,但是我可以處理它們。)有趣的是,PString表不需要存在於數據庫模式中 - 返回的hrefs實際上並不涉及到數據庫中的實際數據項目。

請注意,這不會回答如何使用@Query註釋執行此操作的問題。我通過返回列表再次嘗試,但仍然有相同的錯誤。

+0

這種方法對我來說效果不好,你找到了解決方案嗎?如果是的話,你可以在這裏回答:http://stackoverflow.com/questions/33538426/spring-data-rest-sdr-bug-persistent-entity-must-not-be-null – 2015-11-05 07:10:46

相關問題