I have recently started using Scala, Akka and the Salat serialization library for MongoDB. Now I am trying to set up a SalatDAO for objects that look like this:
import com.novus.salat.annotations._
case class MyObject(@Key("_id) compId: MyCompositeId, value: String)
case class MyCompositeId(x: String, y: String)
and I created a DAO like the following:
import com.novus.salat.global._
import com.novus.salat.dao._
import com.mongodb.casbah.{MongoURI, MongoConnection}
import com.mongodb.casbah.commons.MongoDBObject
class MyObjectDAO(uri: String, db: String, coll: String)
extends SalatDAO[MyObject, MyCompositeId](MongoConnection(MongoURI(uri))(db)(coll))
Finally I wrote a simple unit test:
val dao = new MyObjectDAO(...)
val id = new MyCompositeId("some","key)
// works fine
dao.findOne(MongoDBObject("_id.x" -> id.x, "_id.y" -> id.y))
// does NOT work
dao.findOneById(id)
the first call will return Some(_)
while the second one will return None
.
Now I looked at the source code for findOneById
to try and understand why it's not working:
/** @param id identifier
* @return (Option[ObjectType]) Some() of the object found,
* or <code>None</code> if no such object exists
*/
def findOneById(id: ID) =
collection.findOneByID(id.asInstanceOf[AnyRef]).map(_grater.asObject(_))
And it smells a lot like a bug to me: the method is passing to collection.findOneByID()
an object of type MyCompositeId
which the Casbah driver doesn't understand. I believe Salat is supposed to serialize this object to a MongoDBObject before passing it.
Am I correct? Is this a bug? Or is there something I am missing?
Thanks
Discussion on this Salat project ticket: https://github.com/novus/salat/issues/110