• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Spring Roo + Spring Data + MongoDB Repositories Integration

May 8, 2014 //  by Amr Mohammed//  Leave a Comment

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.

Database Model

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.

Spring Roo MongoDB Initial Project

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

Mongo Setup

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

MongoDB Entities

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:

Mongo Repository

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.

Mongo Application View

Mongo Employees Mongo Addresses

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.

Download Source Code

[wpdm_file id=88]

Category: Spring FrameworkTag: Eclipse, MongoDB, Spring Data, Spring ROO

About Amr Mohammed

Previous Post: «jquery JQuery remove(), removeAttr() and removeClass() Example
Next Post: JQuery Contains() Selector Example jquery»

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact