Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource

static.springsource.org
from static.springsource.org More from this publisher
13.07.2015 Views

please define productname in your docbook file!completion, loading the document from MongoDB the first time the value is used in your application.Here is an example of a simple Entity that has a field annotated with @RelatedEntity.@Entitypublic class Customer {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String firstName;private String lastName;@RelatedDocumentprivate SurveyInfo surveyInfo;// getters and setters omitted}Example 8.4 Example of Entity with @RelatedDocumentpublic class SurveyInfo {private Map questionsAndAnswers;public SurveyInfo() {this.questionsAndAnswers = new HashMap();}public SurveyInfo(Map questionsAndAnswers) {this.questionsAndAnswers = questionsAndAnswers;}public Map getQuestionsAndAnswers() {return questionsAndAnswers;}public void setQuestionsAndAnswers(Map questionsAndAnswers) {this.questionsAndAnswers = questionsAndAnswers;}}public SurveyInfo addQuestionAndAnswer(String question, String answer) {this.questionsAndAnswers.put(question, answer);return this;}Example 8.5 Example of domain class to be stored as documentOnce the SurveyInfo has been set on the Customer object above the MongoTemplate that wasconfigured above is used to save the SurveyInfo along with some metadata about the JPA Entity is storedin a MongoDB collection named after the fully qualified name of the JPA Entity class. The following code:1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 91

please define productname in your docbook file!Customer customer = new Customer();customer.setFirstName("Sven");customer.setLastName("Olafsen");SurveyInfo surveyInfo = new SurveyInfo().addQuestionAndAnswer("age", "22").addQuestionAndAnswer("married", "Yes").addQuestionAndAnswer("citizenship", "Norwegian");customer.setSurveyInfo(surveyInfo);customerRepository.save(customer);Example 8.6 Example of code using the JPA Entity configured for cross-store persistenceExecuting the code above results in the following JSON document stored in MongoDB.{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ),"_entity_id" : 1,"_entity_class" : "org.springframework.data.mongodb.examples.custsvc.domain.Customer","_entity_field_name" : "surveyInfo","questionsAndAnswers" : { "married" : "Yes","age" : "22","citizenship" : "Norwegian" },"_entity_field_class": "org.springframework.data.mongodb.examples.custsvc.domain.SurveyInfo" }Example 8.7 Example of JSON document stored in MongoDB1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 92

please define productname in your docbook file!completion, loading the document from <strong>MongoDB</strong> the first time the value is used in your application.Here is an example of a simple Entity that has a field annotated with @RelatedEntity.@Entitypublic class Customer {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String firstName;private String lastName;@RelatedDocumentprivate SurveyInfo surveyInfo;// getters and setters omitted}Example 8.4 Example of Entity with @RelatedDocumentpublic class SurveyInfo {private Map questionsAndAnswers;public SurveyInfo() {this.questionsAndAnswers = new HashMap();}public SurveyInfo(Map questionsAndAnswers) {this.questionsAndAnswers = questionsAndAnswers;}public Map getQuestionsAndAnswers() {return questionsAndAnswers;}public void setQuestionsAndAnswers(Map questionsAndAnswers) {this.questionsAndAnswers = questionsAndAnswers;}}public SurveyInfo addQuestionAndAnswer(String question, String answer) {this.questionsAndAnswers.put(question, answer);return this;}Example 8.5 Example of domain class to be stored as documentOnce the SurveyInfo has been set on the Customer object above the MongoTemplate that wasconfigured above is used to save the SurveyInfo along with some metadata about the JPA Entity is storedin a <strong>MongoDB</strong> collection named after the fully qualified name of the JPA Entity class. The following code:1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!