James Williams

Using MongoDB with Morphia and Groovy

Tags: General

As I mentioned in this post, I live coded a blog site using Morphia and MongoDB in my workshop session at MongoSF. I used Grails as the platform to quickly scaffold the application. One might wonder why I didn't use the Grails plugin. I used Morphia because it aligns with traditional Java paradigms and didn't want to have too many new items by using a full Groovy stack.


After installing MongoDB, you will need a version of the Java driver and the Morphia library. Make sure to use the latest snapshot.

Creating the mapped objects

Morphia uses Java annotations to describe how to persist objects in the database. The following annotations are available:

  • @Id - marks the MongoDB id field to be autogenerated
  • @Entity - marks the class to be persisted as an object and optionally the collection
  • @Embedded - tells Morphia to embed this object in another
  • @Reference - analogous to MongoDB DBRef
  • @Indexed - prepare an index for this property
  • @Serialized - store as BSON
  • @Property - indicates that the property name in MongoDB will be different than in the object
  • @Transient - doesn't persist the property

As you may already know, Groovy adds a few properties to objects. You will get an error that it won't persist the unmapped fields but it doesn't inhibit execution. Our BlogEntry class contains a title field, date it was created, content, a MongoDB document id and an embedded list of comments.

import com.google.code.morphia.annotations.*

public class BlogEntry implements Comparable{
    @Id  private String id

    String title
    Date dateCreated = new Date()
    String content

    List<comment> comments = [ ]

Similarly, our Comment object contains properties for the name, dateCreated, and content. Please note that both places that refer to Comment need the @Embedded annotation.

import com.google.code.morphia.annotations.*

public class Comment {
    String name
    Date dateCreated = new Date()
    String content

While you can marshal/unmarshal objects from POJO/POGOs to BasicDBObjects and persist them to a collection by hand, it is much simpler to use a data access object(DAO) which provides all the CRUD functions for you.

import com.mongodb.Mongo
import com.google.code.morphia.*

public class EntryDAO extends DAO {
    public EntryDAO(Morphia morphia, Mongo mongo) {
       super(mongo,morphia, "entries")

In this snippet, I initialize a DAO object, create BlogEntry and Comment objects, persist them to the DAO, and read them back out to prove they were stored. Please excuse the Java-ness of the code but I didn't want to blow people's minds with too much Groovy.

import com.mongodb.*
import com.google.code.morphia.Morphia

mongo = new Mongo()
morphia = new Morphia()
dao = new EntryDAO(morphia, mongo)

entry = new BlogEntry()
entry.setTitle("Test Entry")
entry.setDateCreated(new Date())
entry.setContent("This is a test entry.")

comment = new Comment()
comment.setDateCreated(new Date())
comment.setContent("Test comment")

entry.comments = []

entries = dao.find()
entries.each {
    println it.properties

With a running instance of MongoDB, you could run all these bits in a Groovy console and see it in action. In the next post, I'll cover how to integrate these classes into a Grails application.