2014-05-20 44 views
0

我有一個模塊A,通過用戶,組和相關類提供身份驗證。該模塊使用org.springframework.data:spring-data-jpa:1.6.0.RELEASE從數據庫訪問此數據。值得注意的是,模塊A使用通過擴展JpaRepositoryFactoryBean配置的自定義BaseRepository,但是移除它並不能解決下面的問題。如何在spring-data-jpa(2x)+ spring-rest-webmvc中選擇MappingContext?

第二個模塊B也有一些類和存儲庫來管理,與模塊A類無關,再次使用spring-data-jpa進行存儲,但連接到不同的數據庫。該項目使用org.springframework.data:spring-data-rest-webmvc:2.1.0.RELEASE通過REST公開其存儲庫。模塊B使用模塊A中的類對用戶進行身份驗證,但不處理這些類實例,也不存儲任何引用。

現在我遇到的問題是,當模塊A不存在(或者使用的是尚未使用spring-data-jpa的舊版本)時,我的模塊B REST API可以完美地工作,但是當它出現時,與下面的堆棧跟蹤創建自引用鏈接:

java.lang.IllegalArgumentException: Cannot create self link for class Document! No persistent entity found! 
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.getSelfLinkFor(PersistentEntityResourceAssembler.java:81) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:64) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:32) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 
at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:144) ~[spring-data-commons-1.8.0.M1.jar:na] 
at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:96) ~[spring-data-commons-1.8.0.M1.jar:na] 
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:220) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.resultToResources(AbstractRepositoryRestController.java:207) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 
at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:135) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na] 

參見:https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java#L80

它看起來在RepositoryFactoryBeanSupport是在討論到錯誤MappingContext,即使我的org.springframework.data.repository.support.Repositories包含了所有從模塊A的repositoryBeanNames和模塊B.

有誰知道我可以如何強制使用特定的MappingContext,也許通過我的RepositoryRestMvcConfiguration的擴展?

** 編輯 **
下面是說明問題的GitHub的庫:
https://github.com/timtebeek/dual-data-jpa-rest-webmvc

它,因爲據報道,在該數據其餘項目中的錯誤:
https://jira.spring.io/browse/DATAREST-312

+0

你還在2.1.0.RELEASE中看到這個嗎? –

+0

Wauw,euhm,我會檢查;我使用的Spring 4與2.1.0.RELEASE版本有衝突,所以我需要切換回3.2.x來測試一下。一旦我有一個工作版本,我會盡快更新。 – Tim

+0

@OliverGierke我可以證實這個問題也發生在spring-data-jpa:1.6.0.RELEASE和spring-data-rest-webmvc:2.1.0.RELEASE中。是否有任何其他信息可以幫助您進行調試? – Tim

回答

0

這發生在我身上

我試圖查詢一個特定的實體

我解決它創建類

在你的情況的資料庫,它會是

@Repository 
public interface DocumentRepository extends JpaRepository<Document, Long> { 
} 

也做所有需要的配置使用JPA存儲庫。 Look here

我希望肝。

+0

感謝您的建議,但恐怕問題有點複雜這裏。你可以[看看我用來說明這個問題的git回購](https://github.com/timtebeek/dual-data-jpa-rest-webmvc/)。它[已經包含類似於你的片段的類](https://github.com/timtebeek/dual-data-jpa-rest-webmvc/blob/master/documents/src/main/java/com/github/timtebeek/ ddjrw /文件/回購/ DocumentRepo.java)。這個問題僅出現在它與第二個Spring-data-jpa項目結合時,如錯誤報告中所示:https://jira.spring.io/browse/DATAREST-312 – Tim