I`ve发现一些问题,非常接近我的问题(例如Play Framework/Scala: abstract repository and Json de/serialization),但他们没有解决我的问题。播放2 /斯卡拉 - 通用反应蒙戈CRUD - JSON序列
什么我真的想要实现我的CRUD DAO为了共同的CRUD操作的抽象。
我建立一个GenericMongoDaoActor为:
abstract class GenericMongoDaoActor[T <: Entity: Writes](implicit inj:Injector, implicit val f:Format[T]) extends Actor with Injectable {
protected val db = inject[DefaultDB]
protected val collectionName: String
protected val collection:JSONCollection
//set to None to use fallback
def defaultSelector(obj:T):Option[JsObject] = None
def fallbackSelector(obj:T) = Json.obj("_id" -> Json.obj("$elemMatch" -> obj._id))
protected def find(jsObject: JsObject) = {
val currentSender = sender
val futureOption = collection
.find(jsObject)
.cursor[T](ReadPreference.primaryPreferred)
.headOption
futureOption.mapTo[Option[T]].flatMap {
case Some(pobj) =>
currentSender ! Some(pobj)
Future{pobj}
case None =>
currentSender ! None
Future{None}
}
}
protected def save(obj:T):Unit = {
update(obj, true)
}
protected def update(obj:T):Unit = {
update(obj, false)
}
private def update(obj:T, upsert: Boolean):Unit = {
val selector = defaultSelector(obj) match {
case None => fallbackSelector(obj)
case Some(sel) => sel
}
collection.update(
selector,
obj,
GetLastError.Default,
upsert).onComplete {
case Failure(e) => Logger.error("[" + this.getClass.getName + "] Couldn`t update " + obj.getClass.getName + ": " + Json.prettyPrint(Json.toJson(obj)), e)
case Success(lastError) => //currentSender ! lastError todo: do something with lastError
}
}
def findAll() = {
collection.find(Json.obj()).cursor[T](ReadPreference.primaryPreferred).collect[List]()
}
}
的DAOActor处理继承抽象类 “实体” 的实体:
abstract class Entity {
val _id: BSONObjectID
}
目前有2类,继承实体..
正如你可以看到我的DOAActor已经背景下必然要寻找的范围写道T] ..
abstract class GenericMongoDaoActor[T <: Entity: Writes]
当我尝试构建我的项目时,它抱怨没有OWrites在update方法中序列化“T”类型的“obj”。
No Json serializer as JsObject found for type T. Try to implement an implicit OWrites or OFormat for this type.
collection.update(<-------------
我找不到解决此问题的方法。如果可以,请告诉我。
这解决了我的问题。非常感谢。我仍然不明白为什么它抱怨说,它需要在上面owrites或oformat .. – muenchnair