AWS Database Blog
Accelerate application development with Morphia ODM and Amazon DocumentDB
Morphia’s object document mapping capabilities provide Amazon DocumentDB (with MongoDB compatibility) developers massive productivity gains, faster application development, and cleaner code bases by drastically reducing the boilerplate and complexity typically associated with persistent data access. Morphia takes care of bidirectional Java object and Amazon DocumentDB document mapping, automating transparent persistence operations. This enables developers to interact directly with familiar Java constructs, avoiding the need to navigate JSON-based structures and query syntax. You can simply focus on your core domain logic and building your target applications rather than wrestling with the intricacies of low-level Java driver APIs. The result is saved time, reduced lines of code, minimal invasive changes needed to existing object models, and increased agility to iterate on features.
Amazon DocumentDB is a scalable, highly durable, and fully managed database service for operating mission-critical JSON workloads for enterprises. Amazon DocumentDB simplifies your architecture by providing built-in security best practices, continuous backups, and native integrations with other AWS services. Morphia is an open source Object Document Mapper (ODM) for Java developers that provides a convenient way to map Java objects to Amazon DocumentDB documents.
In this post, we discuss how to develop an application using Morphia ODM with Amazon DocumentDB. Additionally, the integration steps highlight how Morphia streamlines working with Amazon DocumentDB behind the scenes through automated object document mapping.
Solution overview
Morphia is an ODM library built as a wrapper around Java drivers. ODM is a programming technique used to facilitate the interaction between object-oriented programming languages (like Java) and document-oriented databases (like Amazon DocumentDB and MongoDB), which store data in a flexible, schema-less manner. Morphia provides a way to map Java objects to Amazon DocumentDB documents and vice versa. This means that you can work with Amazon DocumentDB using Java objects and their corresponding methods, rather than directly dealing with low-level JSON queries and documents.
The following diagram illustrates the solution architecture.
Prerequisites
To complete this configuration, you need the following:
- An Amazon DocumentDB cluster. You can integrate your Amazon DocumentDB cluster with a Java application using Morphia ODM libraries. If you don’t already have an Amazon DocumentDB cluster, see Get Started with Amazon DocumentDB to create a new cluster.
- Java 17 installed.
- Maven installed.
You may incur costs in your account related to Amazon DocumentDB. You can use the AWS Pricing Calculator to estimate the costs.
Create a Morphia Java project with Maven
From your AWS Cloud9 environment, launch a new terminal window by choosing Window and New Terminal. To generate a new template Java project, run the following command in the terminal window:
Alternatively, you can use the docdb-morphia-starter
sample application from the GitHub repo.
Add Maven dependencies
You need to add Morphia to your project. You can download the pre-built JARs for Morphia from Maven Central and use them in your Java project.
You can also use a dependency management tool like Maven. Add the following to the dependencies section in your pom.xml file:
Morphia is a wrapper around the MongoDB Java driver that depends on the Java driver. Add the following Java driver dependency to your pom.xml file:
Define your data model
Morphia uses models called entities
that map to documents in a collection. It has a convenient annotation syntax that you can use to define those models and even create indexes.
To store and retrieve data from an Amazon DocumentDB database, let’s create a POJO model or entity class. This entity represents a collection in Amazon DocumentDB and uses annotations to define the collection name, attributes, keys, and indexes.
In this example, you create a products
collection and a corresponding model object, which stores the details of products in a catalog
database. You create a product model with six attributes: id
, name
, sku
, description
, inventory
, and category
.
Use the @Entity
annotation to instruct Morphia that the following class will be used as an entity. By default, Morphia maps an entity to a collection in Amazon DocumentDB by the name of its class, but if you want to override this default name, you can add a name to @Entity
annotation.
In the following example, we use @Entity("products")
to specify the collection name as products.
You can specify the document’s primary key _id
using the @Id
annotation. If you don’t specify anything, Amazon DocumentDB generates an _id
field while creating the document.
Other fields are left unannotated. By default, Morphia maps the fields in the Entity
object to the attributes with the same name in an Amazon DocumentDB collection, but you can override this.
Inside the project’s directory, create a new file named Product.java
within a package path matching com.example.morphia.docdb.entities
. Copy the Java code from the sample project to this new file.
Connect with TLS enabled
To connect to a TLS-enabled Amazon DocumentDB cluster from a Java-based application, your program must use the AWS-provided certificate authority (CA) file to validate the connection. To use the Amazon Relational Database Service (Amazon RDS) CA certificate, refer to Connecting with TLS Enabled.
Configure the database and connection
To establish a connection to your Amazon DocumentDB cluster using Morphia libraries, you first need to retrieve your Amazon cluster endpoint. Complete the following steps:
- On the Amazon DocumentDB console, in the navigation pane, choose Clusters.
- Find your cluster and choose the Regional cluster identifier.
- On the Connectivity & security tab, copy the command for connecting to your cluster with an application.
- Remove
&tlsCAFile=global-bundle.pem
from the copied connection string. Note that you have already imported the AWS-provided CA file into the trust store.
Now you can create your connection. - Find the connection string for your Amazon DocumentDB cluster and add it as a constant in your
App.java
class. Then add the name of the database to use as a constant for the class. As a best practice, you can externalize these constants to an external configuration file.With the connection string now configured, the next step is to set the keystore properties for establishing a secure connection.
- Use the keystore in your application by setting the following system properties in your
App.java
class:The connection string is now configured and the key store properties are set. You can now connect to the Amazon DocumentDB cluster using Morphia.
The following example demonstrates how to initialize a Morphia instance. You can then use this instance to configure entity mappings.
- In your
App.java
class, add the following code to connect to the Amazon DocumentDB database. When you’re connected to the database, you can also configure the data store that you created.
All the entities you defined within the com.example.morphia.docdb
package in the previous step have now been mapped to documents in the collections.
Now that your entities are mapped to collections in the database, you can start interacting with the database.
Perform CRUD operations with Morphia
Let’s explore basic CRUD (Create, Read, Update, Delete) operations using the Morphia library and test each of these operations one by one.
Save or create
First, you can add a few documents to the collection using the save
method. The save
method takes a Product object as input and saves the product document in an Amazon DocumentDB collection. Add the following code snippet to your App.java
class:
Read
In this example, you perform two read operations. You fetch a product by name and find a list of products based on the category.
You will add two simple methods in the App.java
class. The first method, findProductByName
, queries the collection based on the name
attribute. The second method, findAllProductsByCategory
, retrieves all the documents of a particular category, as shown in the following code:
Update
You can update an existing document with the update
or modify
method on the Query
instance. These update operations are run on the server without fetching any documents across the wire. Update operations are defined using a set of functions as defined on UpdateOperators. In this example, you query an existing product by SKU and increment the inventory by 10. Add the following method to your App.java
class:
Delete
In this example, you add two delete operations. First, delete one product by SKU. In the second method, you delete all documents (products) in the collection. Add the following methods to your App.java
class:
Run and test your application
You can now invoke all the CRUD operations defined in the previous sections from within the main function in the App.java
file. For the full implementation of the main function, refer to the completed App.java file in our sample project. This file contains the final, executable main method for running the application.
Run your Morphia-based sample application with the following Maven command:
You’re done! You have successfully connected to Amazon DocumentDB using Morphia ODM libraries from a Java application.
Clean up
To avoid ongoing costs, complete the following steps to clean up your resources:
- If you created a new Amazon DocumentDB cluster, delete the cluster.
- If you used an existing Amazon DocumentDB cluster, run the following commands to delete the database and collections created by the sample application from the mongo shell:
- If you created an AWS Cloud9 IDE, delete the environment.
Conclusion
In this post, you learned about using Morphia as an Object Document Mapper for Java applications that need to interface with Amazon DocumentDB. Specifically, you learned that Morphia provides a high-level abstraction layer that removes the need to use low-level Java driver APIs or write complex database access code. By using the Morphia libraries and following simple configuration steps to map Java objects to database documents, you can focus on your domain models and business logic rather than database plumbing.
You can find the example used in this post on GitHub. If you have feedback about this post, share it in the comments section.
About the authors
Gururaj S Bayari is a Senior DocumentDB Specialist Solutions Architect at AWS. He enjoys helping customers adopt Amazon’s purpose-built databases. He helps customers design, evaluate, and optimize their internet-scale and high-performance workloads powered by NoSQL or relational databases.
Anshu Vajpayee is a Senior DocumentDB Specialist Solutions Architect at AWS. He has been helping customers adopt NoSQL databases and modernize applications using Amazon DocumentDB. Before joining AWS, he worked extensively with relational and NoSQL databases for around 15 years.