Created
May 14, 2012 01:18
-
-
Save anonymous/2691137 to your computer and use it in GitHub Desktop.
Salat nested case class with scala 2.9.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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