2017-03-15 2 views
1

Spring Boot and Mongo DBの新機能です.PHJOクラスをモデル化しようとしています。患者調査データを保存し、維持する必要があります。調査データは、JSONとして第三者から来て、任意の番号を含んでいます。質問と回答と任意のタイプの質問もあります。Spring Data Mongo DBを使ってクラスをモデル化する方法フィールドと型の長さを変えて入れ子になったJSONドキュメントを格納する方法

入れ子になったJSONを単一のエンティティ/オブジェクトとして保存するにはどのようなデータ型/アノテーションを使用しますか? Mongo DBがJSONをBSONとして保存していると聞きました。しかし、どうやってそれをすることができますか?

は、現在、私のモデルクラスは、ここでは、この

@Document 
public class Patient { 

    @Id private String id; 


    private String pID; 
    private String firstName; 
    private String lastName; 

    @DBRef 
    private List<Survey> surveys; 

    public Patient() { } 

    public Patient(String fName, String lName) 
    { 
     this.firstName = fName; 
     this.lastName = lName; 
    } 

    public String getpID() { 
     return pID; 
    } 

    public void setpID(String pID) { 
     this.pID = pID; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
} 



@Document 
public class Survey { 

    @Id private String id; 

    private String pID; 

    private Document surveyData; 

    public Survey(String pID, Document surveyData) 
    { 
     this.pID = pID; 
     this.surveyData = surveyData; 
    } 

    public String getpID() { 
     return pID; 
    } 

    public void setpID(String pID) { 
     this.pID = pID; 
    } 

    public Document getSurveyData() { 
     return surveyData; 
    } 

    public void setSurveyData(Document surveyData) { 
     this.surveyData = surveyData; 
    } 

} 

@RepositoryRestResource(collectionResourceRel = "Survey", path = "survey") 
public interface SurveyRepository extends MongoRepository<Survey, String> { 

    public Survey findBypID(@Param("pID") String pID); 

} 

のように見える、私はこれをテストするために使用していJSONです。

{ 
"pID": "test1", 
"surveyData" : { 
    "resourceType": "QuestionnaireResponse", 
    "questionnaire": { 
    "reference": "http://hl7.org/fhir/2016Sep/cqif/questionnaire-cqif-example.xml.html" 
    }, 
    "item": [ 
    { 
     "linkId": "Q1", 
     "text": "Little interest or pleasure in doing things", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q2", 
     "text": "Feeling down, depressed, or hopeless", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q3", 
     "text": "Trouble falling or staying asleep", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q4", 
     "text": "Feeling tired or having little energy", 
     "answer": [ 
     { 
      "valueString": "More than half days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q5", 
     "text": "Poor appetite or overeating", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q6", 
     "text": "Feeling bad about yourself - or that you are a failure or have let yourself or your family  down", 
     "answer": [ 
     { 
      "valueString": "More than half days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q7", 
     "text": "Trouble concentrating on things, such as reading the newspaper or watching television", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q8", 
     "text": "Moving or speaking so slowly that other people could have noticed. Or the opposite - being  so fidgety or restless that you have been moving around a lot more than usual", 
     "answer": [ 
     { 
      "valueString": "More than half days" 
     } 
     ] 
    }, 
    { 
     "linkId": "Q9", 
     "text": "If you checked off any problems, how difficult have these problems made it for you to  do your work, take care of things at home, or get along with other people", 
     "answer": [ 
     { 
      "valueString": "Several Days" 
     } 
     ] 
    } 
    ] 
} 

} 

私は取得しています例外は、今、私はJavaモデルクラスに埋め込まれたJSONオブジェクトを格納することができる午前「オブジェクト」にsurveyDataのデータ型を変更することにより、

2017-03-15 17:18:19.257 ERROR 15364 --- [nio-8080-exec-3] o.s.d.r.w.RepositoryRestExceptionHandler : Could not read document: Can not deserialize Class org.springframework.data.mongodb.core.mapping.Document (of type annotation) as a Bean 
at [Source: [email protected]; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize Class org.springframework.data.mongodb.core.mapping.Document (of type annotation) as a Bean 
at [Source: [email protected]; line: 1, column: 1] 
+0

"JSONをGSONとして保存する" - BSONはGsonではなくBSONを意味すると思われる - Gsonはライブラリであり、BSONはデータフォーマットです。問題は 'gson'タグスコープの外にあるようです。 –

+0

調査のサンプル文書を提供できますか? –

+0

こんにちはMarkus、はい、私はBSON – Vis

答えて

0

です。

タイプとして 'ドキュメント'が注釈であるため無効です。

関連する問題