2016-05-03 37 views
0

我是SpringData的新手,我沒有看到發生在這裏的事情。我創建了擴展PagingAndSortingRepository接口和overrided像這樣的findAll()方法:SpringData findAll正在處理所有數據庫表,而不是所要求的

@Override 
@Query 
List<MyEntity> findAll(); 

我打電話我的服務這個方法,但它使我的應用程序拋出一個異常Caused by: java.lang.StackOverflowError因爲該方法是閱讀通過整個數據庫,不僅從數據庫中的MyEntity表。任何想法?

+0

打開EclipseLink日誌記錄並顯示您的實體。它可能是一個複雜的對象圖,其中熱切希望獲取的關係迫使提供者在對象模型中引入所有引用的實體。 – Chris

回答

0

顯然問題出在EclipseLink的配置中。在persistence.xml中,我添加了這一行<shared-cache-mode>NONE</shared-cache-mode>,現在它可以正常工作。

0

無需重寫getall()方法。在您的服務自動裝配的dao或存儲庫類,並使用此可以直接調用findall()方法。 在你想寫自定義方法的情況下,除了Spring數據jpa給出的內容之外,我們使用@Query來編寫自定義查詢。

+0

謝謝你的回答,但我是問別的東西:) –

+0

歡迎@AlbanoVito –

0
No Need to Override. What i have done is i have created a repository i.e 
FavoriteRepository which is extending JpaRepository and i have mentioned the 
dbmodel name (Favorite) 

like this JpaRepository<Favorite, Long> // Here Favorite is my model name 
and Long is the type of primary key mentioned in db model Favorite as @Id 

@Repository 
public interface FavoriteRepository extends JpaRepository<Favorite, Long>{ 

} 

Now you can use method findOne or findAll. As these methods are present in 
Jparepository.Hope so it will help 

If you want to add new method then use @Query with JpQL 
@Query(value = "select f from Favorite f where f.userId=:userId ") 
public List<Favorite> getFavoritesForUser(@Param("userId") String userId); 
相關問題