玩! framework 2.0 scala-ClassCastException:无法将models.MyModel强制转换为models.MyModel [英] Play! framework 2.0 scala - ClassCastException: models.MyModel cannot be cast to models.MyModel
问题描述
这是我的第一个play 2.0应用程序,scala对我来说仍然很新,因此我可能在某个地方犯了一个错误.我正在使用捆绑了Salat和Casbah的全新插件: https://github.com/leon/play -salat
This is my first play 2.0 app, and scala is still pretty new to me, so I'm likely making a mistake somewhere. I'm using a pretty new plugin that bundles Salat and Casbah: https://github.com/leon/play-salat
我已经简化并重命名了所有内容以使其通用.
I've simplified and renamed everything to make it generic.
我的视图(views/MyController/search.scala.html
):
@(modelList:List[models.MyModel])
@main(title = "Search MyModel") {
<table>
@for(a <- modelList) {
<tr><td>@a.field<td>@a.field2</li>
}
</table>
}
我的控制器(controllers/MyController.scala
):
package controllers
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import com.mongodb.casbah.Imports._
import models._
object MyController extends Controller {
def search = Action {
val modelList = MyModel.all.toList;
Ok(views.html.MyController.search(modelList))
}
}
我的模特:(models/MyModel.scala
):
package models
import play.api.Play.current
import java.util.{Date}
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.mongodb._
import se.radley.plugin.mongodb.salat._
case class MyModel(
id: ObjectId = new ObjectId,
field: String,
field2: String
)
object MyModel extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
def all = find(MongoDBObject())
}
我收到此错误:
ClassCastException: models.MyModel cannot be cast to models.MyModel
对我来说没有什么意义-有人遇到过这种情况吗?
Which doesn't make much sense to me--has anyone run into something like this?
完整堆栈跟踪:
play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[ClassCastException: models.MyModel cannot be cast to models.MyModel]]
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: java.lang.ClassCastException: models.MyModel cannot be cast to models.MyModel
at views.html.MyController.search$$anonfun$apply$1.apply(search.template.scala:25) ~[classes/:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:0.11.2]
at scala.collection.immutable.List.foreach(List.scala:45) ~[scala-library.jar:0.11.2]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
推荐答案
我认为这里的问题是您的MyModel集合对象
I think the problem here is that your MyModel collection object
object MyModel extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
def all = find(MongoDBObject())
}
是您模板中要导入的内容.
Is what is being imported in your template.
我会尝试以下操作:
package models
import play.api.Play.current
import java.util.{Date}
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.mongodb._
import se.radley.plugin.mongodb.salat._
case class MyModel(
id: ObjectId = new ObjectId,
field: String,
field2: String
)
object MyModelDAO extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
def all = find(MongoDBObject())
}
案例类已经随同对象一起提供.在这种情况下,scala会为您生成一个MyModel
伴随类.扩展了正确键入的SalatDAO
的对象也称为MyModel
.您必须查看为case类和MyModel extends SalatDAO[MyModel, ObjectId]
类生成的字节码,以查明生成的类名称是什么,我想对于您的case类,其为models.MyObject$
.但是,如果您使用不同的名称,那么您应该得到想要的结果.
Case classes already come with companion objects. In this case a MyModel
companion class is generated by scala for you. The object extending the properly typed SalatDAO
is also named MyModel
. You would have to look at the bytecode generated for the case class and the MyModel extends SalatDAO[MyModel, ObjectId]
classes to find out what the generated class name is, I think it would be models.MyObject$
for your case class. But if you name it differently, then you should get the result you are looking for.
这篇关于玩! framework 2.0 scala-ClassCastException:无法将models.MyModel强制转换为models.MyModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!