Search code examples
phantom-dsl

Phantom's generated `store` method throws a ClassCastException at runtime


I have the following Phantom table definition:

package myPackage

import com.outworkers.phantom.CassandraTable
import com.outworkers.phantom.connectors.RootConnector
import com.outworkers.phantom.dsl._
import com.outworkers.phantom.keys.{PartitionKey, PrimaryKey}

import scala.concurrent.Future

case class KeysTwoThreeAndFour(myKeyTwo: Int, myKeyThree: String, myKeyFour: Int)

abstract class MyTable extends CassandraTable[MyTable, Int] with RootConnector {
  object myKeyOne extends IntColumn(this) with PartitionKey
  object myKeyTwo extends IntColumn(this) with PrimaryKey
  object myKeyThree extends StringColumn(this) with PrimaryKey
  object myKeyFour extends IntColumn(this) with PrimaryKey
  object myValue extends IntColumn(this)

  def insertValue(myKeyOne: Int, valuesMap: Map[KeysTwoThreeAndFour, Int]): Future[Unit] = {
    val resultFutures = for ((key: KeysTwoThreeAndFour, myValue) <- valuesMap) yield {
      store(myKeyOne, key.myKeyTwo, key.myKeyThree, key.myKeyFour, myValue).future()
    }


    Future.sequence(resultFutures).map { _ => () }
  }
}

This compiles fine, but at runtime throws the following exception:

java.lang.ClassCastException: scala.Tuple5 cannot be cast to scala.runtime.Nothing$
    at myPackage.MyTable$anon$macro$1$1.store(MyTable.scala:10)
    at com.outworkers.phantom.CassandraTable.store(CassandraTable.scala:125) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
    at myPackage.MyTable$$anonfun$2.apply(MyTable.scala:19)
    at myPackage.MyTable$$anonfun$2.apply(MyTable.scala:18)
    ...

I am following the examples in the bottom of the Phantom table docs, what seems to be the problem? Is the issue perhaps that I have a simple Int as my "Record" type instead of an actual class?

I am using phantom-dsl 2.7.6, Play Framework 2.3.10 and Scala 2.11.11.

Note that the following code works fine:

insert
.value(_.myKeyOne, myKeyOne)
.value(_.myKeyTwo, key.myKeyTwo)
.value(_.myKeyThree, key.myKeyThree)
.value(_.myKeyFour, key.myKeyFour)
.value(_.myValue, myValue)
.future()

Thanks.


Solution

  • It seems that the issue was due to the "Record" type being a primitive, instead of a class; in other words, wrapping my Int in a case class having that Int as the only member solved the problem.

    Additionally, there was a problem with the Select statement, which was solved too:

    java.util.concurrent.ExecutionException: Boxed Error
        at com.outworkers.phantom.CassandraTable.fromRow(CassandraTable.scala:85) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
        at com.outworkers.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:24) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
        at com.outworkers.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:24) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
        at com.outworkers.phantom.builder.query.SelectQuery.fromRow(SelectQuery.scala:59) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
        at com.outworkers.phantom.builder.query.RootExecutableQuery$class.singleResult(ExecutableQuery.scala:176) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]
        at com.outworkers.phantom.builder.query.SelectQuery.singleResult(SelectQuery.scala:33) ~[com.outworkers.phantom-dsl_2.11-2.7.6.jar:2.7.6]