PrimeFaces + Spring Roo + Spring Data + MongoDB Integration Example

This tutorial will introduce the integration of Primefaces, Spring Data & MongoDB frameworks. This tutorial assumes the you are familiar with the Spring Roo for creating the spring applications. Also, I assume that you are familiar with the Mongo DB. If you’re not, it’s good for you to go back into the below tutorials for getting the all of mentioned important principles. By the way, all of the required steps for creating a project and configure it for using Roo Shell are also covered inside this tutorial.

Note : This tutorial focuses only on creating the project using Spring Roo. we will not explain the code snippets since everything managed by Spring Roo.

Also read:

1. Tools Used

  • JDK 1.6.
  • Spring Roo 1.2.4. (It’s required for compatibility)
  • Maven 3.
  • Tomcat 7.35.
  • MongoDB.

2. Database Model

Here is the MongoDB collections that is used for persisting the documents used in this tutorial.

Address Document Collection Structure

Employee Document Collection Structure

3. Setup Mongo Persistence Layer

By executing mongo setup –databaseName JavaBeat –host –port 27017 instruction, the persistence layer will be added and configured to your project.

Setting of Mongo Persistence

4. Creating Entities

For creating the required entities, you should follow the below steps:

  • By executing the entity mongo –class –identifierType java.math.BigInteger instruction, an Address entity has been created.
  • Add the addressCountry field by executing field string –fieldName addressCountry –notNull instruction.
  • Add the addressCity field by executing field string –fieldName addressCity –notNull instruction.
  • By executing the entity mongo –class –identifierType java.math.BigInteger instruction, an Employee entity has been created.
  • Add the employeeName by executing field string –fieldName employeeName –notNull instruction.
  • Add the address reference into Employee entity by executing field reference –fieldName address –type –notNull instruction.

Creating Mongo Entities

5. Create Spring Data Repository

Spring Data Repository setup for MongoDB doesn’t differ from what’s happening in the Spring Roo & JPA. Here you just use a mongo keyword.

For creating Spring Data Repository, you have to execute the following commands:

  • For creating Employee Repository execute repository mongo –interface ~.roo.repo.EmployeeRepository –entity
  • For creating Address Repository execute repository mongo –interface ~.roo.repo.AddressRepository –entity

Mongo Repositories

6. Setup PrimeFaces Web Layer

The creating of web layer using JSF/Primefaces is identical to what you’ve achieved before in a previous tutorials. The different here, is that, we would create a Primefaces layer rather using of Spring MVC. By executing web jsf setup –implementation ORACLE_MOJARRA –library PRIMEFACES –theme EGGPLANT instruction, you’ve requested from Spring Roo to setup the required configuration files as well as the theme that would be used.

Setup Primefaces Configuration

7. Create Managed Beans

For creating managed beans inside Spring Roo, all what you have to do is to execute the web jsf all –package ~.roo.web instruction.

Primefaces Managed Beans Created

Once, you’ve done creating the web application, you need to comment the Spring OpenEntityManagerInViewFilter and its filter mapping. That change is required to be able to running the application.

8. Build WAR using Maven

Navigate into your project directory using Windows command and execute the mvn clean package for generating the required WAR.

Mongo Primefaces Artifact Generation

9. Install the Application Into Tomcat 7

Now, just copy the .war generated file into your Tomcat7 directory’s webapp folder and start your tomcat using the startup.bat.

War Inside Tomcat 7 Webapp

10. Spring Roo + Spring Data + PrimFaces + MongoDB Demo

MongoDB Primefaces Home

MongoDB All Addresses

MongoDB All Employees

MongoDB Add Employee

MongoDB Added Employee Successfully

11. MongoDB Documents

MongoDB Address Document

MongoDB Employee Documents

12. Summary

You’ve just developed a PrimeFaces web application that adhered the Spring Data repositories using Spring Roo. Your application is capable of persisting against MongoDB. If you have any questions, please write it in the comments section

Download Source Code

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!