【问题标题】:Phantom's generated `store` method throws a ClassCastException at runtimePhantom 生成的 `store` 方法在运行时抛出 ClassCastException
【发布时间】:2017-05-16 16:48:25
【问题描述】:

我有以下幻像表定义:

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 { _ => () }
  }
}

这编译正常,但在运行时抛出以下异常:

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)
    ...

我正在关注 Phantom table docs 底部的示例,似乎有什么问题?问题可能是我有一个简单的Int 作为我的“记录”类型而不是一个实际的类吗?

我正在使用phantom-dsl 2.7.6、Play Framework 2.3.10 和 Scala 2.11.11。

请注意,以下代码可以正常工作:

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

谢谢。

【问题讨论】:

  • 你为什么要从你的Table返回Int
  • 我的表由 4 个键列作为键,并存储一个 Int 值。

标签: phantom-dsl


【解决方案1】:

问题似乎是由于“记录”类型是原始类型,而不是类;换句话说,将我的 Int 包装在一个以该 Int 作为唯一成员的案例类中解决了这个问题。

另外,Select语句有问题,也解决了:

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2023-03-18
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多