2016-05-24 27 views
0

我需要讀取包含豆類(ChildDocument)的列表文件(不是parentDocument)。而這些豆類,我只需要一個特定的字段轉換內容)更改值。 它僅用於讀取mongoDb並加載bean,而不是存儲在mongodb中的修改。彈簧數據MongoDB的轉換器不工作

例如:

@Document(collection = "parent") 
    class ParentDocument { 

    @Id 
    String id; 

    List<ChildDocument> children; 
} 

    @Document 
    class ChildDocument { 

    @Id 
    String id; 

    // Modify the content reading in the database before storing the result in the java bean 
    String content; 
} 

我嘗試在ChildDocument使用轉換器,但它不工作,因爲轉換器不用於像ChildDocument嵌入文檔(我想?)。

有人能幫助我,有一個乾淨的解決方案做到這一點?

感謝

+0

你能證明一下,你到目前爲止試過的嗎? –

回答

0

我的解決辦法是:

  • 父子
  • 創建文檔轉換器註冊一個轉換器,用於父親
  • 在父親的轉換方法,子轉換器必須被實例化作爲參數只需要源爲孩子
  • 分配裏面的實例化父文檔響應轉換器父親

那麼,所有你需要的是轉換觸發孩子轉換的父文檔。

再見......

+0

感謝您的回答。它解決了我的問題;-) –