Using MongoDB in a Java EE7 Framework

The central point of creating an application to perform in an enterprise environment is that the application must be secure, portable, and highly available. It also must be able to interact with diverse systems, yet manageable from a prime location. JEE7 is an important merger framework for all the features; It works pretty seamlessly with MongoDB. The article goes hands on in creating a Web application using MongoDB as the storage.

The Arrangement Is...

It's a simple, lean CRUD application, or rather a part of it, using some of the stalwarts of JEE7 such as EJB and JSF. The idea is to reposit a Candidate in MongoDB, search required Candidates according to the skill one has, and also will be able to remove a particular Candidate from the database altogether.

What You Need...

A JEE application runs in a container that provides the norms of the enterprise application along with all necessary components. This container, in our case, is the application server WildFly. So, apart from the Netbeans IDE, and JDK, we need a WildFly Application Server. Also:

Google-Gson: This is an external Java Library useful for converting Java Objects into their JSON representation and vice-versa. This peripheral library is optional, but convenient on occasional circumstances.

Note that any IDE and Application Server can be used, provided it is JEE7 compatible.

Once downloaded/installed, a Web Application project in Netbeans can be created as follows.

Open NetBeans, File, New Project...

Figure 1: Opening a new project

Give a name of the Project.

Figure 2: Naming the project

Apply proper Server settings.

Figure 3: Applying the proper Server settings

Apply the JavaServer Faces framework. Finish.

Figure 4: Applying the JavaServer Faces framework

Once the project has been created, include gson-x.x.x.jar and mongo-java-driver-3.2.1.jar as an external library to the project.

Candidate.java

This class represents the schema for the database. This structure shall be used to create MongoDB storage, as follows:

CandidateFacade.java

This class implements CRUD operations with the MongoDB through a JDBC Driver. This class is designated as stateless because here we do not want to perform the work represented by this class to span across multiple method invocations. Also, stateless beans are lightweight and easy to manage. However, this is just one representation. Other representations are equally possible under various circumstances.

CandidateController.java

This is the managed bean class that acts as a controller to the JSF pages. The @EJB annotation declaration represents a reference in the component namespace where the container performs the injection (Context Dependency Injection, CDI). The object, once created by the container, can access the data transaction operations defined in the CandidateFacade class. The annotation @PostConstruct ensures that the dependency injected is initialized before use in the JSF pages. Observe how the Candidate list gets populated with pre-reposited data in the MongoDB database.

index.xhtml

Once the server side part is complete, we need a view for inserting a new document as well as displaying the Candidate details. The page also must contain a button to perform a filter on the skill set of the Candidate list and a delete button, respectively. The following is the index.xhtml page, which needs to be added to the project's web pages

That's it. What we get on running the application is shown in Figure 5.

Figure 5: Application running in a Web browser

Conclusion

This project, though, does not provide a comprehensive guide to enterprise development, and yet can be used as a template for building applications that require MongoDB with JEE capabilities. The improvements in the JEE specification make application development hassle free with minimal code and little configuration.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.