Using Spring Data with MongoDB: Creating a Model

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.

  1. Create a POJO class Catalog in the com.mongo.model package.
  2. Add fields for id, journal, edition, publisher, title, and author and the corresponding get/set methods.
  1. Annotate the id field with @Id.
  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *