Next, create the model class to use with the Spring Data MongoDB project. A domain object to be persisted to MongoDB server must be annotated with @Document.
- Create a POJO class Catalog in the com.mongo.model package.
- Add fields for id, journal, edition, publisher, title, and author and the corresponding get/set methods.
- Annotate the id field with @Id.
- Add a constructor that may be used to construct a Catalog instance.
The Catalog entity is listed below.
package com.mongo.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Catalog {
@Id
private String id;
private String journal;
private String publisher;
private String edition;
private String title;
private String author;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJournal() {
return journal;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Catalog(String id, String journal, String publisher, String edition,
String title, String author) {
id = this.id;
this.journal = journal;
this.publisher = publisher;
this.edition = edition;
this.title = title;
this.author = author;
}
}
Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.