Play 2.0 和 MongoDB 与 Salat 接口 [英] Play 2.0 and MongoDB interfacing with Salat

查看:44
本文介绍了Play 2.0 和 MongoDB 与 Salat 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Play 2.0 应用程序不断收到此错误:

I keep getting this error with my Play 2.0 application:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[MatchError: 4f7f4ae4251735803a942b2c (of class org.bson.types.ObjectId)]]
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:82) [play_2.9.1.jar:2.0]
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:63) [play_2.9.1.jar:2.0]
    at akka.actor.Actor$class.apply(Actor.scala:290) [akka-actor.jar:2.0]
    at play.core.ActionInvoker.apply(Invoker.scala:61) [play_2.9.1.jar:2.0]
    at akka.actor.ActorCell.invoke(ActorCell.scala:617) [akka-actor.jar:2.0]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:179) [akka-actor.jar:2.0]
Caused by: scala.MatchError: 4f7f4ae4251735803a942b2c (of class org.bson.types.ObjectId)
    at com.novus.salat.transformers.in.LongToInt$class.transform(Injectors.scala:216) ~[salat-core_2.9.1-0.0.8-SNAPSHOT.jar:0.0.8-SNAPSHOT]
    at com.novus.salat.transformers.in.package$$anon$31.transform(Injectors.scala:180) ~[salat-core_2.9.1-0.0.8-SNAPSHOT.jar:0.0.8-SNAPSHOT]
    at com.novus.salat.transformers.Transformer$$anonfun$transform_$bang$1.apply(Transformer.scala:71) ~[salat-core_2.9.1-0.0.8-SNAPSHOT.jar:0.0.8-SNAPSHOT]
    at com.novus.salat.transformers.Transformer$$anonfun$transform_$bang$1.apply(Transformer.scala:71) ~[salat-core_2.9.1-0.0.8-SNAPSHOT.jar:0.0.8-SNAPSHOT]
    at scala.Option.flatMap(Option.scala:146) ~[scala-library.jar:0.11.2]
    at com.novus.salat.transformers.Transformer.transform_$bang(Transformer.scala:71) ~[salat-core_2.9.1-0.0.8-SNAPSHOT.jar:0.0.8-SNAPSHOT]

我的模型非常简单,我使用的是 SalatDao 风格的数据库访问:

My model is very simple and I use SalatDao-flavored DB access:

case class Gossip(@Key("_id") id: org.bson.types.ObjectId, title: String, link: String, description: String, permalink: String,
                  image: String, date: String) {}
object GossipDAO extends SalatDAO[Gossip, ObjectId](
  collection = MongoConnection()("gossips")("items"))

调用时抛出错误:

val gossips = GossipDAO.find(ref = MongoDBObject("modificationDate" -> MongoDBObject("$gte" -> startDate))).toList

我已经通过一些其他脚本将数据加载到我的 MongoDB,它们似乎没问题,因为我能够在 MongoDB shell 中找到它们:

I've loaded data to my MongoDB via some other script, and they seem to be OK, since I'm able to find them in MongoDB shell with:

 db.items.find({_id:ObjectId("4f7f4ae4251735803a942b2c")})

更重要的是,如果我更改 startDate 条件以便没有对象匹配它,我的代码运行良好.这表明查询是正确的,但是从 MongoDBObject 到 Scala 的 Gossip 类的转换失败了.

What's more if I change the startDate condition so as no objects match it, my code runs fine. This indicates that the query is correct but the transformation from MongoDBObject to Scala's Gossip class fails.

上面的代码有问题吗?

更新

  • 我的依赖只有这个:

  • My dependencies are only this:

val appDependencies = Seq("com.mongodb.casbah" %% "casbah" % "2.1.5-1","com.novus" %% "salat-core" % "0.0.8-SNAPSHOT")

val appDependencies = Seq( "com.mongodb.casbah" %% "casbah" % "2.1.5-1", "com.novus" %% "salat-core" % "0.0.8-SNAPSHOT" )

我使用'modificationDate',因为它是mongoDB集合对象中的一个字段.

I use 'modificationDate', because it is a field in the mongoDB collection object.

当我执行 db.items.find({_id:ObjectId("4f7f4ae4251735803a942b2c")}) 我得到这个:

When I execute db.items.find({_id:ObjectId("4f7f4ae4251735803a942b2c")}) I get this:

https://gist.github.com/2928862

  • 如果您只是获取整个集合,是否会发生同样的问题?

我不知道如何使用 salat DAO 获取所有对象.但是当我的 case 类没有 'id' 键时,我只能在 MongoConnection 上执行 db.find() ,它会返回所有可用的对象并将它们转换为 Model 对象.相同的解决方案,当我添加 'id' 键时产生与上述相同的错误.

I don't know how to fetch all objects with salat DAO. But when I had my case class without 'id' key, I was able to do just db.find() on MongoConnection and it would return all the available objects and convert them into Model objects. The same solution, when I've added 'id' key produced the same error as above.

推荐答案

好吧,删除'target'目录并再次运行Play20似乎解决了这个问题.那里肯定有一些旧的课程信息.

Well, it seems that deleting 'target' directory and running Play20 again solved the issue. Must have been some old classes info in there.

这篇关于Play 2.0 和 MongoDB 与 Salat 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆