Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2012 01:18
Show Gist options
  • Save anonymous/2691137 to your computer and use it in GitHub Desktop.
Save anonymous/2691137 to your computer and use it in GitHub Desktop.
Salat nested case class with scala 2.9.1
import com.mongodb.casbah._
import com.novus.salat.grater
import com.novus.salat.annotations._
import com.novus.salat.global._
import com.mongodb.casbah.Implicits._
case class Child(value: String)
case class Parent(@Key("_id") id: String, nested: Child)
object TestNesting {
def main(args: Array[String]) {
val db = MongoConnection()("childtest")
val coll = db("testColl")
coll.drop
val child = Child("C")
val parent = Parent("testId", child)
// A conversion straight back to Parent will work here as the DBObject has
// the Child, not a DBObject of the child
println(grater[Parent].asDBObject(parent))
// On save, the Child class gets flattened to a DBList because
// it implements Product
coll.save(grater[Parent].asDBObject(parent))
coll.findOneByID("testId") map { result =>
// This row will cause an error as it expects type "Child" and receives
// A db list [ "C" ]
println(grater[Parent].asObject(result))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment