Using Kundera with MongoDB: The Kundera-Mongo JPA Client Class

In the following subsections we shall install the Maven project and generate Eclipse IDE files for use with the Maven project. Subsequently we shall run the Kundera-Mongo JPA client class, which was discussed in the preceding section “Running JPA CRUD operations,” to invoke the different methods of the client class. The KunderaClient class used in this chapter is listed:

package kundera;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

public class KunderaClient {

private static EntityManager em;

private static EntityManagerFactory emf;

public static void main(String[] args) {

emf = Persistence.createEntityManagerFactory(“kundera”);

em = emf.createEntityManager();

create();

findByClass();

query();

update();

delete();

close();

}

private static void create() {

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”);

em.persist(catalog);

catalog = new Catalog();

catalog.setCatalogId(“catalog2”);

catalog.setJournal(“Oracle Magazine”);

catalog.setPublisher(“Oracle Publishing”);

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

catalog.setTitle(“Ouintessential and Collaborative”);

catalog.setAuthor(“Tom Haunert”);

em.persist(catalog);

}

private static void findByClass() {

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

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

}

private static void query() {

javax.persistence.Ouery query = em

.createOuery(“SELECT c FROM Catalog c”);

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

for (Catalog catalog : results) {

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

System.out.println(“\n”);

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

}

}

private static void update() {

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

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

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

System.out.println(“After updating”);

System.out.println(“\n”);

query();

}

private static void delete() {

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

em.remove(catalog);

System.out.println(“After removing catalog1”);

query();

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

System.out.println(“\n”);

System.out.println(“After removing all catalog entries”);

query();

}

private static void close() {

em.close(); emf.close();

}

}

1. Installing the Maven Project

In the preceding section we developed the source code for a Maven project. In this section we shall build the Maven project, including generating the dependency JAR KunderaMongoDB-1.0.0.jar for the Maven project.

Right-click on pom.xml in Project Explorer and select Run As ► Maven install as shown in Figure 9-16 to build and install KunderaMongoDB-1.0.0.jar in the local repository in the target subdirectory.

Maven scans for projects and builds and installs the KunderaMongo project as shown in Figure 9-17. The output from the Maven install should include the message “BUILD SUCCESS” If not, some error has occurred and the Maven project has not been installed. The /root/workspace/KunderaMongoDB/target/ KunderaMongoDB-1.0.0.jar gets built and installed.

Next, we shall use the Maven Eclipse plugin to generate Eclipse IDE files for use with the Maven project. We shall run the following goals, listed in Table 9-5, from the Maven Eclipse plug-in.

We need to create a run configuration for the eclipse:clean eclipse:eclipse goals.

  • Right-click on pom.xml and select Run As ► Run Configuration as shown in Figure 9-18.

  • Right-click on Maven Build and select New.
  • For the new run configuration, specify the following values and click on Apply.
    • Name: Eclipse (for example)
    • Base directory: The KunderaMongoDB project base directory
    • Goals: eclipse:clean eclipse:eclipse
  •  Subsequently click on Run as shown in Figure 9-19

If the Maven goals do not generate an error, a BUILD SUCCESS message should get output as shown in Figure 9-20.

In this section we shall run the KunderaClient class in the KunderaMongoDB project. We shall use the Maven exec:java goal from the Exec Maven plug-in to run the KunderaClient class. We specified the class to run using the mainClass parameter to the exec-maven-plugin configuration in pom.xml.

We shall run the exec:java goal in Eclipse to run the KunderaClient class. But, first we need to create a new run configuration in Eclipse

 

  1. Right-click on Maven Build in Run Configurations and New.
  2. Specify a Name (KunderaClient, for example) and a goal (exec:java) and click on Apply and subsequently on Run as shown in Figure 9-21.

The KunderaClient application gets run and the output for the invoked methods gets generated such as finding and listing entities. In the next section we shall invoke the KunderaClient class methods to create, find, update, and delete entities.

2. Invoking the KunderaClient Methods

Now it is time to invoke the methods.

  1. First, invoke the create() method in the main method and comment out the other methods. When the Run Configuration for exec:java is run the KunderaClient application runs and the create() method gets invoked to create some entities.
  2. Subsequently, run the db.catalog.find() method in Mongo shell to list the two entities added to MongoDB as shown in Figure 9-22.

  1. Next, invoke the findByClass() method in the main method by uncommenting the method invocation.
  2. Run the exec:java goal run configuration again. The catalog1 entity fields get listed as shown in Figure 9-23.

  1. Next, invoke the query() method using the exec:java run configuration. The output from running the Maven project run configuration is shown in Figure 9-24.

  1. Next, invoke the update() method to update the journal field.
  2. Run the exec:java goal run configuration. The journal field gets updated and the updated field value get output as shown in Figure 9-25.

  1. Next, invoke the delete() method and run the KunderaClient application using the run configuration for exec:java. As the output in Figure 9-26 indicates, the entities get listed after removing one and after removing both entities.

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 *