Java Reference
In-Depth Information
Once the mapping works, finding all vampire movies that have a critic's consensus is as
simple as the following script:
GMongo mongo = new GMongo()
def db = mongo.getDB('movies')
db.vampireMovies.find([critics_consensus : ~/.*/]).each { movie ->
println Movie.fromJSON(movie)
}
It's hard to be much simpler than that. Working with MongoDB [ 20 ] is just as easy as using
a traditional relational database. [ 21 ]
20 A detailed treatment of MongoDB is contained in the topic MongoDB in Action (Manning, 2011) by Kyle Banker:
www.manning.com/banker/ .
21 For some reason, none of the Twilight movies were returned from the “vampire” query. I thought about fixing
that, and ultimately decided it wasn't a bug, but a feature.
8.6. Summary
Virtually every significant application requires persistent data. The vast majority of those
arebasedonrelational databases.IntheJavaworld,relational persistence useseitherJDBC
or an object-relational mapping tool like Hibernate or JPA. This chapter reviewed both ap-
proaches and examined how Groovy can simplify them.
The Groovy Sql class removes most of the clutter that accompanies raw JDBC. Any code
that uses JDBC directly can be significantly simplified using the Sql class.
Many modern applications use JPA for persistence, especially with Hibernate as the under-
lying API and the Spring framework to handle singletons and transactions. Just configuring
such an application is a nontrivial task. On the other hand, the Grails framework handles
all of it elegantly and with a minimum of effort.
Finally, many so-called NoSQL databases have a Java API. Some, like MongoDB, include
a Groovy wrapper that makes working with the underlying databases simple.
 
 
Search WWH ::




Custom Search