Using MongoDB with Morphia and Groovy

Posted     

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.

Setup

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.*

@Entity
public class BlogEntry implements Comparable{
	@Id  private String id
	
	String title
	Date dateCreated = new Date()
	String content
	
	@Embedded
	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.*

@Embedded
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 = []
entry.comments.add(comment)

dao.save(entry)
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.

 


Related Posts:
The Best Programming Advice I Ever Got
Google Code Jam 2013 - Fair And Square
How to get a tech book deal
Visor and TotalFinder bring awesome to OS X

Comments

  1. Alireza said:

    Nice Post ;) Thanks for sharing
  2. dresses said:

    It is good to have the ability to read a good quality blog with useful data on topics that a lot are interested on. The point that the data indicated are all first hand on actual experiences even help more. Continue doing what you do as we love reading your work.
  3. megazoll said:

    Thanks for article! As soon you create the post about MongoDB & Grails intergation?
  4. replica rolex paypal said:

    We are the best
blog comments powered by Disqus