2012-03-23 21 views
3

我只是試圖讓「添加自定義行爲的所有庫」例如,從reference doc。但對於下面的類:春數據的MongoDB例如工作不工作

public class MyRepositoryImpl<T, ID extends Serializable> 
    extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID> { 

    public void sharedCustomMethod(ID id) { 
    // implementation goes here 
    } 
} 

我得到的編譯錯誤

()發現SimpleJpaRepository沒有合適的構造
構造org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaRepository (java.lang.Class,javax.persistence.EntityManager)不適用
(實際和正式參數列表長度不同)
構造函數org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaR epository(org.springframework.data.jpa.repository.support.JpaEntityInformation,javax.persistence.EntityManager)不適用
(實際的和正式的參數列表的長度不同)

我怎樣才能得到這個工作?

回答

-1

首先,這是一個簡單的編譯錯誤作爲超有你要複製或提供自己的一個構造函數。其次,你似乎混合了它的JPA和MongoDB模塊。您寧願要擴展SimpleMongoRepository

+0

可以請你提供一個工作的例子嗎? – Bobo 2012-03-28 19:48:23

+0

只需按照參考文檔並將Spring Data JPA相關類型與Spring Data MongoDB的等價物交換即可。 – 2012-03-29 22:02:14

+0

我跟着參考文檔,並沒有看到如何實現無參數構造函數。 – Roger 2012-05-16 22:25:52

-1

什麼@Oliver說的話是,你複製/粘貼代碼不正確。 Spring-Data-MongoDB Docs複製/粘貼Spring-Data-JPA Docs的那一段代碼,並忘記更改它。如果你真的看了你的代碼,改變很簡單。

public interface MyMongoRepository<T, ID extends Serializable> extends MongoRepository<T, ID> { 

    void sharedCustomMethod(ID id); 
} 


public class MyImplMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements MyMongoRepository<T, ID> { 

    public void sharedCustomMethod(ID id) { 
     // implementation goes here 
    } 
} 

這是否說清楚了?這只是文檔中的一個錯字。對於「添加自定義行爲所有存儲庫」使用彈簧數據的MongoDB

0

合適的解決方案是在這post詳細。

按照上面的帖子中描述的步驟後,你可以有你的任何資料庫界面的自定義共享庫接口擴展如下

@Repository 
public interface CustomerRepository extends MongoRepository<Customer, String>, 
     WootideRepositoryCustom<Customer, String> { 
} 

在WootideRepositoryImpl提供的實施將是可用CustomerRepository。

它對我來說非常好。

希望,因爲它是參考所述第一位置彈簧數據mongodb的文檔被更新。

0

我不鹹,但實施這是不幸的不明確。

看一個完整的示例圖像,github上: https://github.com/mpereira-dev/spring-data-mongo-shared-repo-example

要點:

  1. 必須的基本接口上使用@NoRepositoryBean所以彈簧不會爲它創建一個bean 。
  2. 基本接口的名稱與將自定義方法添加到單個回購庫時的名稱無關。
  3. 實現類的名稱也無所謂
  4. 添加@Repository實現類將產生以下錯誤:構造

參數0 com。示例。 demo.repository.ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo 需要一個類型爲 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' 的bean無法找到。

  • 您必須使用:
  • @EnableMongoRepositories( repositoryBaseClass = ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo.class)

    告訴春天有關的基礎類如果你不這樣做,你會得到這個錯誤(春天試圖解析方法到查詢中):

    造成者: org.springframework.data.mapping.PropertyReferenceException:No 屬性someMethod在Person類型中找到!

  • 確保您的包結構如下春天約定因此所有組件可以通過彈簧被發現,其他明智的樂趣配置:@ComponentScan,@EntityScan和@EnableMongoRepositories。
  • A picture says a thousand words

    7.6.2。添加自定義行爲所有存儲庫

    https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories