Entries with the tag General

Java at MongoSF

03 May, 2010

As has come to be expected the 10gen-ers have put on a great conference. It's almost like a MongoDB world domination tour with the NoSQL Live in Boston this March, MongoSF and MongoNYC this month and MongoEU this summer. There seemed to be bigger numbers of Ruby and Python devs but that wasn't so surprising given that they tend be early adopters compared to Java devs. Because of that I believe the path to reach Java developers is to show them that they don't have to throw away all their code to use MongoDB. My morning session capitalized on that and used Grails to show how you could quickly wire up persistence using DAOs with Morphia and MongoDB.The afternoon session that was a bit more high level primer for what was covered in the morning. 

 
The schwag was well thought out too. Speakers got really snazzy embroidered MongoDB polos. There was an update of the mugs from NoSQL Live featuring the MongoDB branding on the one side with 10gen branding on the other. 
 
Slides from the conference are here. Videos from the sessions should be coming soon. Check the 10gen blog for updates.
 
 
It was also great news to learn that there are several books on MongoDB set to release this fall. 
 
 

Using MongoDB with Morphia and Groovy

05 May, 2010

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.