Play! framework 2.0 scala - ClassCastException: models.MyModel cannot be cast to models.MyModel

你。 提交于 2019-12-07 12:43:50

问题


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.

My view (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>
}

My controller (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))
  }
}

My model: (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())
}

And I'm getting this error:

ClassCastException: models.MyModel cannot be cast to models.MyModel

Which doesn't make much sense to me--has anyone run into something like this?

Full stack trace:

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]

回答1:


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.

I would try the following:

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())
}

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.



来源:https://stackoverflow.com/questions/10132730/play-framework-2-0-scala-classcastexception-models-mymodel-cannot-be-cast-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!