Using Kundera with MongoDB: Running CRUD Operations

In the next few subsections we shall create a catalog in the catalog collection and we shall add data to the catalog, find a catalog entry, update a catalog entry, and delete a catalog entry.

1. Creating a Catalog

In this section we shall add some documents to the catalog collection in MongoDB.

  1. Add a method called create() to the KunderaClient class and invoke the method from the main method so that when the application is run the method gets invoked.

The JPA API is defined in the javax.persistence package. The EntityManager interface is used to interact with the persistence context. The EntityManagerFactory interface is used to interact with the entity manager factory for the persistence unit. The persistence class is used to obtain an EntityManagerFactory object in a Java SE environment.

  1. Create a EntityManagerFactory object using Persistence class static method cre ateEntityManagerFactory(java.lang.String persistenceUnitName).
  2. Create an EntityManager instance from the EntityManagerFactory object using the createEntityManager() method.

EntityManagerFactory emf = Persistence.createEntityManagerFactory(“kundera”);

em = emf.createEntityManager();

  1. In the create() method create an instance of the entity class Catalog. Using the set methods set the catalogId, journal, publisher, edition, title, and author fields.

Catalog catalog = new Catalog();

catalog.setCatalogId(“catalog1”);

catalog.setJournal(“Oracle Magazine”);

catalog.setPublisher(“Oracle Publishing”);

catalog.setEdition(“November-December 2013”);

catalog.setTitle(“Engineering as a Service”);

catalog.setAuthor(“David A. Kelly”);

  1. Make the domain model instance managed and persistent using the persist(java.lang.Object entity) method in the EntityManager interface.

em.persist(catalog);

Similarly, other JPA instances may be persisted. When we run the KunderaClient class in a later section the Catalog entities get persisted to the MongoDB database.

2. Finding a Catalog Entry Using the Entity Class

The EntityManager class provides several methods for finding an entity instance. In this section we shall find a Catalog entity instance using the find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) method in which the first parameter is the entity class and the second parameter is the _id field value for the document to find.

  1. Add a method called findByClass() to the KunderaClient class and invoke the method from the main method so that when the application is run the method gets invoked.
  2. Invoke the find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) method using Catalog.class as the first arg and “catalog1″ as the second arg.

Catalog catalog = em.find(Catalog.class, “catalog1”);

  1. Invoke the get methods on the Catalog instance to output the entity fields.

System.out.println(catalog.getJournal());

System.out.println(catalog.getPublisher());

System.out.println(catalog.getEdition());

System.out.println(catalog.getTitle());

System.out.println(catalog.getAuthor());

The Catalog entity field values shall get output when the client class is run.

3. Finding a Catalog Entry Using a JPA Query

The Query interface is used to run a query in the Java Persistence query language and native SQL. The EntityManager interface provides several methods for creating a Query instance. In this section we shall run a Java Persistence query language statement by first creating an instance of Query using the EntityManager method createQuery(java.lang.String qlString) and subsequently invoking the getResultList() method on the Query instance.

  1. Add a method called query() to the KunderaClient class and invoke the method from the main method so that when the application is run the method gets invoked.
  2. In the query() method invoke the createQuery(java.lang.String qlString) method to create a Query instance.
  1. Supply the Java Persistence query language statement as SELECT c FROM Catalog c. javax.persistence.Query query = em.createQuery(“SELECT c FROM Catalog c”);
  2. Invoke the getResultList() method on the Query instance to run the SELECT statement and return a List<Catalog> as result.

List<Catalog> results = query.getResultList();

  1. Iterate over the List object using an enhanced for statement to output the fields of the Catalog instance.

for (Catalog catalog : results) {

System.out.println(catalog.getCatalogId());

System.out.println(catalog.getJournal());

System.out.println(catalog.getPublisher());

System.out.println(catalog.getEdition());

System.out.println(catalog.getTitle());

System.out.println(catalog.getAuthor());

}

The Catalog entity field values shall get output when the client class is run.

4. Updating a Catalog Entry

In this section we shall update a catalog entry using the Java Persistence API. The persist() method in EntityManager may be used to persist an updated entity instance.

  • Add a method called update() to the KunderaClient class and invoke the method from the main method so that when the application is run the method gets invoked. For example,
    • To update the edition column in document with primary key “catalog1″ create an entity instance for the catalog1 row using the find(java.lang.

Class<T> entityClass, java.lang.Object primaryKey) method.

    • Subsequently set the edition field to the updated value using the setEdition method.
    • Persist the updated Catalog instance using the persist(java.lang.Object entity) method.

Catalog catalog = em.find(Catalog.class, “catalog1”);

catalog.setEdition(“Nov-Dec 2013”);

em.persist(catalog);

  • The Java Persistence query language provides the UPDATE clause to update a row. Create a Query instance using an UPDATE statement and the createQuery(String) method in EntityManager. Subsequently invoke the executeUpdate() method to execute the UPDATE statement.

em.createQuery(“UPDATE Catalog c SET c.journal = ‘Oracle-Magazine'”). executeUpdate();

  • The journal column in all the rows in the catalog column family gets updated. Having applied updates invokes the query() method to output the updated field values.

When the KunderaClient class is run the updated field journal should have the updated value Oracle-Magazine instead of Oracle Magazine.

5. Deleting a Catalog Entry

In this section we shall remove documents persisted in MongoDB using the Java Persistence API. The remove(java.lang.Object entity) method in EntityManager may be used to remove an entity instance.

  1. Add a method called delete() to the KunderaClient class and invoke the method from the main method so that when the application is run the method gets invoked.
  1. To remove the document with primary key “catalogl” create an entity instance for the catalogl row using the find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) method. Subsequently invoke the remove(java.lang.Object entity) method to remove the document with primary key catalog1 from MongoDB.

Catalog catalog = em.find(Catalog.class, “catalog1”);

em.remove(catalog);

  1. The Java Persistence query language provides the DELETE clause to delete a document. Create a Query instance using an DELETE statement and the createQuery(String) method in EntityManager. Subsequently invoke the executeUpdate() method to execute the DELETE statement.

em.createQuery(”DELETE FROM Catalog c”).executeUpdate();

All rows get deleted. The DELETE statement does not delete the document itself but deletes all the columns in the rows.

  1. Having applied deletes, either using the remove(java.lang.Object entity) or using the DELETE Java Persistence query language statement invoke the query() method to output any Catalog instances persisted to catalog table.

When the client class is run no field values should get listed when the query() method is invoked subsequent to deleting the Catalog entries.

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 *