Using Kundera with MongoDB: Creating a JPA Entity Class

The domain model for a JPA object/relational mapping application is defined in a JPA entity class. The domain model class is just a plain old Java object (POJO) that describes the Java object entity to be persisted, the object properties, and the MongoDB database and collection to persist to. In this section we shall create a JPA entity class for object/relational mapping using Kundera and MongoDB databases. MongoDB, though not a relational database, can be used with object/relational mapping using the Kundera library, which supports some NoSQL databases.

  1. Select File ➤ New ➤ Other.
  2. In the New window, select Java ➤ Class and click on Next as shown in Figure 9-6.

  1. In New Java Class wizard assign the following values as shown in Figure 9-7. Click on Finish.
  • Source folder: KunderaMongoDB/src/main/java
  • Package: kundera
  • Name: Catalog

The Java class kundera.Catalog gets added to the Maven project as shown in Figure 9-8.

  1. Annotate the Catalog class with @Entity annotation to indicate that the class is a JPA entity class. By default the entity name is the same as the entity class name. Annotate the class with @Table to indicate the MongoDB collection name and schema. The table name is the collection name catalog. The schema is in database@persistence-unit format. For the local database and the kundera persistence unit name, which we shall configure in the next section, the schema is local@kundera.

@Entity

@Table(name = “catalog”, schema = “local@kundera”)

  1. The entity class implements the Serializable interface to serialize a cache enabled entity bean to a cache when persisted to a database. To associate a version number with a serializable class by serialization runtime specify a serialVersionUID variable.

private static final long serialVersionUID = 1L;

  1. Annotate the catalogId field with the @Id annotation to indicate that the field is the primary key of the entity. Specify the document field _id as the primary key column using the @Column annotation.

@Id

@Column(name = “_id”) private String catalogId;

The field annotated with @Id must be one of the following types: Java primitive type (such as int, double), any primitive wrapper type (such as Integer, Double), String, java.util.Date, java.sql.Date, java.math.BigDecimal, or java.math.BigInteger.

  1. Add fields journal, publisher, edition, title, and author and annotate them with the @Column annotation to specify the mapping of the fields to columns in the document.

@Column(name = “journal”)

private String journal;

@Column(name = “publisher”)

private String publisher;

@Column(name = “edition”)

private String edition;

@Column(name = “title”)

private String title;

@Column(name = “author”)

private String author;

  1. Add get/set accessor methods for each of the fields. The JPA Entity class is listed below.

package kundera;

import java.io.Serializable;

import javax.persistence.*;

/**

*Entity implementation class for Entity: Catalog

*

*/

@Entity

@Table(name = “catalog”, schema = “local@kundera”)

public class Catalog implements Serializable {

private static final long serialVersionUID = 1L;

@Id

@Column(name = “_id”) private String catalogId;

public Catalog() {

super();

}

@Column(name = “journal”)

private String journal;

@Column(name = “publisher”)

private String publisher;

@Column(name = “edition”)

private String edition;

@Column(name = “title”)

private String title;

@Column(name = “author”)

private String author;

public String getCatalogId() {

return catalogId;

}

public void setCatalogId(String catalogId) {

this.catalogId = catalogId;

}

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;

}

}

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 *