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]
I think the problem here is that your MyModel collection object
Is what is being imported in your template.
I would try the following:
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 typedSalatDAO
is also namedMyModel
. You would have to look at the bytecode generated for the case class and theMyModel extends SalatDAO[MyModel, ObjectId]
classes to find out what the generated class name is, I think it would bemodels.MyObject$
for your case class. But if you name it differently, then you should get the result you are looking for.