ReviewIn the previous section, we have learned how to setup a MongoDB server in Windows and Ubuntu. In this section, we will discuss the project's structure, write the Java classes, and organize them in layers.
Table of ContentsPart 1: Introduction and Functional Specs
Part 2: MongoDB setup
Part 3: Java classes
Part 4: XML configuration
Part 5: HTML Files (with AJAX)
Part 6: Running the Application
Project StructureOur application is a Maven project and therefore follows Maven structure. As we create the classes, we've organized them in logical layers: domain, repository, service, and controller.
Here's a preview of our project's structure:
Domain LayerThis layer contains two classes, User and Role. They represent our database collections, user and role respectively. We're using Spring Data MongoDB to simplify MongoDB access. And to optimize these classes for the framework, we've added the @Document annotation. If you're familiar with JPA, this is similar to the @Entity annotation.
Notice the User class has a reference to a Role property. In order to achieve that, we must annotate the field with @DBRef.
Mapping annotation overview
- @Id - applied at the field level to mark the field used for identiy purpose.
- @Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored.
- @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.
Controller LayerThis layer contains two controllers, MediatorController and UserController
- MediatorController is responsible for redirecting requests to appropriate pages. This isn't really required but it's here for organizational purposes.
- UserController is responsible for handling user-related requests such as adding and deleting of records
Service LayerThis layer contains two services, UserService and InitMongoService
- UserService is our CRUD service for the user collection. All data access is delegated to the repositories
- InitMongoService is used for initiliazing our database with sample data. Notice we're using the MongoTemplate itself instead of the Spring Data MongoDB-based repository. There is no difference between the two. In fact, it's simpler if we used Spring Data instead. But I have chosen MongoTemplate to show an alternative way of interacting with the database
The mapping framework does not handle cascading saves. If you change an Account object that is referenced by a Person object, you must save the Account object separately. Calling save on the Person object will not automatically save the Account objects in the property accounts.
Source: Spring Data MongoDB Reference (7.3.3. Using DBRefs)
Repository LayerThis layer contains two repositories, UserRepository and RoleRepository. These are our data access objects (DAO). With the help of Spring Data MongoDB, Spring will automatically provide the actual implementation. Notice for custom methods we just have to add the method signature.
What is Spring Data MongoDB?
Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities. The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB DBCollection and easily writing a Repository style data access layer
Utility classesTraceInterceptor class is an AOP-based utility class to help us debug our application. This is a subclass of CustomizableTraceInterceptor (see Spring Data JPA FAQ)
NextIn the next section, we will focus on the configuration files for enabling Spring MVC. Click here to proceed.
Share the joy:
Subscribe by reader Subscribe by email Share