Friday, January 20, 2012

Spring MVC 3.1 - Implement CRUD with Spring Data MongoDB (Part 1)

In this tutorial, we will create a simple CRUD application using Spring 3.1 and MongoDB. In particular, we will use Spring MVC to develop the web application and Spring Data MongoDB for the repository.


Dependencies

  • Spring core 3.1.0.RELEASE
  • Spring Data MongoDB 1.0.0.RELEASE
  • MongoDB (server) 2.0.2
  • MongoDB (Java driver) 2.7.2
  • See pom.xml for details

Github

To access the source code, please visit the project's Github repository (click here)

Functional Specs

Before we start, let's define our application's specs as follows:
  • A CRUD page for managing users
  • Use AJAX to avoid page refresh
  • Users have roles. They are either admin or regular (default)
  • Everyone can create new users and edit existing ones
  • When editing, users can only edit first name, last name, and role fields
  • A username is assumed to be unique

Here's our Use Case diagram:
[User]-(View)
[User]-(Add) 
[User]-(Edit) 
[User]-(Delete) 

Database

If you're new to MongoDB and coming from a SQL-background, please take some time to read the following resource SQL to Mongo Mapping Chart. It's a comparison between SQL and MongoDB.

We have two collections (tables in SQL): user and role. The user collection contains personal information of each user; whereas the role collection contains role values for each user where a value of 1 represents an admin and a value of 2 represents a regular user.

Here's the Class diagram:
# Cool UML Diagram
[User|id;firstName;lastName;username;password;role{bg:orange}]1--1> [Role|id;role{bg:green}]

User document

Below is the JSON structure of the User document after Spring Data MongoDB has created the collection:
{ 
   "_id" : "" , 
   "_class" : "org.krams.domain.User"
   "firstName" : ""
   "lastName" : ""
   "username" : ""
   "password" : "" , 
   "role" : { "$ref" : "role" , "$id" : "" }
}
Notice the role property is a reference to a Role record as indicated by $ref: role property.

Role document

Below is the JSON structure of the Role document after Spring Data MongoDB has created the collection:
{ 
   "_id" : ""
   "_class" : "org.krams.domain.Role"
   "role" : ""
}

Screenshots

Let's preview how the application will look like after it's finished. This is also a good way to clarify further the application's specs.

The Activity diagram:

http://yuml.me/diagram/activity/(start)-%3E%3Cd1%3Eview-%3E(Show%20Records)-%3E%7Ca%7C-%3E(end),%20%3Cd1%3Eadd-%3E(Show%20Form)-%3E%7Ca%7C,%20%3Cd1%3Eedit-%3E%3Cd2%3Ehas%20selected-%3E(Show%20Form)-%3E%7Ca%7C,%20%3Cd2%3Eno%20record%20selected-%3E(Popup%20Alert)-%3E%7Ca%7C,%20%3Cd1%3Edelete-%3E%3Cd3%3Ehas%20selected-%3E(Delete%20Record)-%3E%7Ca%7C,%20%3Cd3%3Eno%20record%20selected-%3E(Popup%20Alert)-%3E%7Ca%7C.

Entry page
The entry page is the primary page that users will see. It contains a table showing user records and four buttons for adding, editing, deleting, and reloading data. All interactions will happen in this page.

Entry page

Edit existing record
When user clicks the Edit button, an Edit Record form shall appear after the table.

Edit record form

When a user submits the form, a success or failure alert should appear.

Success alert

When the operation is successful, the update record should reflect on the table.

Edited record appears on the table

Create new record
When a user clicks the New button, a Create New Record form shall appear after the table.

Create new record form

When a user submits the form, a success or failure alert should appear.

Success alert

When the operation is successful, the new record should appear on the table.

New record shows on the form

Delete record
When user clicks the Delete button, a success or failure alert should appear.

Success alert

Reload record
When user clicks the Reload button, the data on the table should be reloaded.

Errors
When user clicks the Edit or Delete button without selecting a record first, a "Select a record first!" alert should appear.

Error alert

Next

In the next section, we will study how to setup a MongoDB server both in Windows and Ubuntu. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC 3.1 - Implement CRUD with Spring Data MongoDB (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

44 comments:

  1. You should have a look to my Spring MVC 3.1 - Spring Data - MongoDB XML free example application https://github.com/sdeleuze/spring-backbone-todo

    ReplyDelete
  2. @Bouiaw, thanks. I'm looking at it right now. It would be great if you can write a blog about this one.

    ReplyDelete
  3. @Bouiaw, the use of XML-less configuration is great. However, I find it anticlimactic when it comes to the repository. I was expecting you would utilize the Spring Data MongoDB repository support, but instead you used directly the MongoTemplate. Though nothing's wrong with that, I find that part somewhat wanting.

    ReplyDelete
  4. Question: Do you know if there is any way to prevent Spring from storing the fully qualified classname under _class in the database (example: "_class" : "org.krams.domain.User") when inserting? Or will the mapping stop working by doing this?

    ReplyDelete
  5. @Anonymous, I have to investigate further if that's possible

    ReplyDelete
  6. I was wondering if you have any example integrating MongoDB with Spring Security.

    ReplyDelete
    Replies
    1. Sorry, I don't have any yet. Is it possible? I think so. You just have to provide a custom user details service which delegates the data access to MongoDB dao or repository.

      Delete
  7. Hi,

    great example that has helped me a lot. What I cannot find anywhere is how to make a multiple-field search form (search by field1, field2... fieldN if they are given values).

    You can search by any combination of any of the fiels (with AND...). I've tried to do it through a "findByField1AndField2AndField3And....". The idea was that if I put "null" in one field, that field would not be used in the search, but Mondo does it, so it cannot be done with this approach.

    How, if any combination of fields to find could be created in the form, could it be done? Do I need to create every combination of "findBy...." that are possible to be searched for?

    Thanks in advance :)

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi, nice work.
    I noticed that the new/create functionality added a new Role for each new user instead of referencing an existing role.

    I fixed this by
    1) removing the role save in UserService.create:
    public User create(User user) {
    user.setId(UUID.randomUUID().toString());
    // user.getRole().setId(UUID.randomUUID().toString());
    ...
    // roleRepository.save(user.getRole());
    return userRepository.save(user);
    }

    2) adding methods
    Role findByRole(Integer role); to RoleRepository
    public Role getRole(Integer role) {
    return roleRepository.findByRole(role);
    } to UserService
    3) replacing the new Role with the role lookup in UserController.create:
    // Role newRole = new Role();
    // newRole.setRole(role);
    Role newRole = service.getRole(role);

    Cheers,
    John

    ReplyDelete
  10. using codebase to a tomcat maven build there appears to be ajax related issues on any type of form submission using your js code. Appears to only work on chrome base browsers. (well having said that only used ff and chromium and it was not working in ff) Didnt bother investigating the underlying issues though. Does work smoothly in chromium.

    ReplyDelete
  11. Hello!

    When I've imported source code on my local PC using Spring Tool Suite, there're errors on jquery-1.6.4.min.js

    ReplyDelete
  12. hi krams,

    Thanks for your work.
    started with the thymeleaf one - brilliant tutorial.

    Now I wanted to look at "Spring Data Redis" and noticed it links to "Spring MVC 3.1 and Spring Data MongoDB".
    accidentally or on purpose?

    ReplyDelete
  13. Good sample.

    but, New and Edit function are not work.
    not apear alert window and reload users Webpage only.
    thank you.

    ReplyDelete
  14. What the hell is this????????????
    I am clicking on the link Spring redis but always the mongoDB tutorial opens. Please try to remove that, for our convenience.
    Thanks and regards

    ReplyDelete


  15. Great Post ! jQuery Tutorial and jQuery Interview Questions with simple and easy examples,Covering JQuery Introduction, Selectors, Hide / Show Effect, Fade Effect, Slide Effect, Animation, Event Handling, Keyboard Event, Mouse Event, Form Events, Traversing, Ajax. HTML5 Tutorial & CSS3 Tutorial.

    ReplyDelete
  16. Is it possible to provide any example integrating redis with mongodb?

    ReplyDelete
  17. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

    ReplyDelete

  18. This is quite educational arrange. It has famous breeding about what I rarity to vouch.
    Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved.
    This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.


    Selenium interview questions and answers
    Selenium Online training
    Selenium training in Pune
    selenium training in USA
    selenium training in chennai

    ReplyDelete
  19. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
    python Training institute in Chennai
    python Training institute in Bangalore
    python Training in Pune

    ReplyDelete
  20. Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
    python Training institute in Chennai
    python Training institute in Bangalore
    python Training in Pune

    ReplyDelete
  21. you have two relational tables on ID, I thought we don't do that with mongo db

    ReplyDelete
  22. greetings.
    im trying to mimic your project folder structure, I started a project with spring initializer and selected my dependencies,including web and mongo but the project came out not having the folder structure and also missing the folder Webapp. please tell me how you got that folder structure.

    ReplyDelete
  23. Thank you for providing the valuable information …
    If you want to connect with AI (Artificial Intelligence) World
    as like

    Python


    RPA (Robotic Process Automation)


    UiPath Training


    Blue Prism


    Data -Science


    ML(Machine Learning)
    related more information then meet on EmergenTeck Training Institute .

    Thank you.!
    Reply

    ReplyDelete
  24. Thank you for providing the valuable information …
    If you want to connect with AI (Artificial Intelligence) World
    as like

    Python


    RPA (Robotic Process Automation)


    UiPath Training


    Blue Prism


    Data -Science


    ML(Machine Learning)
    related more information then meet on EmergenTeck Training Institute .

    Thank you.!
    Reply

    ReplyDelete
  25. Very useful and information content has been shared out here, Thanks for sharing it.aws training in bangalore

    ReplyDelete
  26. Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.informatica training in bangalore

    ReplyDelete
  27. I know that it takes a lot of effort and hard work to write such an informative content like this.cloud computing training in bangalore

    ReplyDelete
  28. Really a awesome blog for the freshers. Thanks for posting the information.microsoft azure training in bangalore

    ReplyDelete
  29. Thank you for sharing .The data that you provided in the blog is informative and effective.microsoft azure training in bangalore

    ReplyDelete
  30. i would love to read more about this Click Here

    ReplyDelete
  31. I like this post,And I figure that they having a great time to peruse this post,they might take a decent site to make an information,thanks for sharing it to me Pretty good post. ExcelR Data Analytics Courses

    ReplyDelete
  32. I can set up my new thought from this post. It gives inside and out data. A debt of
    gratitude is in order for this significant data for all, pleasant bLog! its fascinating.
    much obliged to you for sharing.
    Data Science Training In Pune

    ReplyDelete
  33. Hats off to Realexamdumps.com for the incredible CSA Dumps . It's a well-crafted resource, condensing complex material into manageable sections. The interactive exercises and real-world examples provided a holistic learning experience.Thanks to this i passed my ServiceNow Certified System Administrator exam.

    ReplyDelete
  34. This is a really interesting post. I appreciate the depth of your research and the clarity of your writing. Looking forward to reading more

    Khajuraho Tour Packages
    Western Group of Temples in Khajuraho

    ReplyDelete
  35. This post was exactly what I needed to read today. It’s very well-written and provides practical advice. Keep up the great work

    Destination Wedding Planner Packages
    Mice Tour Operators
    Corporate Event Planner

    ReplyDelete