Since Spring Data includes support for MongoDB repositories, we can use MongoDB as a persistence option when using Roo. We just won’t have the option of using the
active record style for the persistence layer, we can only use the repositories. Other than this difference, the process is very much the same as for a Spring ROO + Spring Data JPA solution.
In this tutorial you will learn how to use Spring Roo with Mongo Database. We have published in our previous tutorial on how to use Spring ROO with JPA. Spring Roo & JPA tutorial provided a common steps to install Spring Roo library, download Spring Roo plugin into your eclipse IDE and create the Spring Roo Project are already explained in our previous tutorial. Please refer that for more details.
I assume that readers who are reading this tutorial would have enough knowledge on working with Spring Data and MongoDB database. If not, please read our previous tutorial about Spring Data & MongoDB
1. Database Model
We’ve already learnt in our previous tutorial about creating a collection inside the MongoDB, so for completing this tutorial; Employee & Address collections should be created for that purpose.
2. Setup Persistence Layer
Setting up the persistence layer steps have been already explained in Spring Roo + JPA. There is no need to repeat here. The next step is to setup the persistence layer through Spring Roo Shell.
The below snapshot should show you the Spring Roo Project Structure.
After setting up the Mongo persistence layer by executing the command mongo setup command inside the Roo shell, the structure of the project folder looks like
Where the database.properties file contains the required information for the database that will be connected to the MongoDB. It’s possible for using the mongo setup –databaseName JavaBeat –host localhost command for creating an already configured file.
3. Create Entities
For creating the entities that will be used for persisting the collections inside MongoDB, you have to follow the below steps:
- Create the Address entity by executing entity mongo –class ~roo.domain.Address inside the Roo Shell.
- Add the addressCountry field by executing field string –fieldName addressCountry –notNull.
- Add the addressCity field by executing field string –fieldName addressCity –notNull.
- Create the Employee entity by executing entity mongo –class ~.roo.domain.Employee.
- Add the employeeName filed by executing field string –fieldName employeeName –notNull.
- Add the address field by executing field reference –fieldName address –type ~.roo.domain.Address –notNull.
- One note here, is that the –activeRecord that already used in the Spring Roo & JPA has been eliminated, because of the Repository in the MongoDB implementation isn’t an optional. Unlike Spring Roo & JPA whose persisting operation could be done through using od standard EntityManagerFactory.
The project structure should be like
4. 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.repository.EmployeeRepository –entity ~.roo.domain.Employee.
- The string token ~ refers to initial package that you’ve provided at the first step, when you create the project as Roo Project.
- For creating Address Repository execute repository mongo –interface ~.roo.repository.AddressRepository –entity ~.roo.domain.Address.
The project structure should look like this:
5. Create Web Layer
For creating a web layer with Spring Roo, you don’t need more than three additional commands. Those additional commands should be executed as their order shown below.
Executing of web mvc setup should setup the basic project structure for a Spring MVC/JSP application.
- Executing of web mvc scaffold –class ~.roo.web.EmployeeController –backingType ~.roo.domain.Employee & web mvc scaffold –class ~.roo.web.AddressController –backingType ~.roo.domain.Address should create the controllers that used by Spring Roo for maintaining the CRUD functionality automatically.
- Using of ~ notion is only for referring the package that you’ve created at the time of the project being created.
- Add into your pom.xml the Spring-dao and Spring-tx dependencies. It’s possible for adding them through using of Spring Roo shell through using of dependency command.
- Navigate into Project menu -> clean.
- Right-Click on the project and choose Run As.
- Select one of the installed Apache Tomcat.
6. Summary
Spring Data is an amazing framework, it provides the Spring Developer the ability to create a full Web Application from scratch. The application integrated with non relational database like MongoDB works much easier with Spring Data and Spring Roo. This tutorial focus on that subject, where an application has been created and integrated with the MongoDB using the Spring Roo tool. Hope you have enjoyed the tutorial. If you have any questions, please write it in the comments section.