MongoDB Scala驱动程序-Set字段有问题

时间:2019-02-19 10:18:08

标签: mongodb scala set codec

我有一个Case类,其中有4套作为字段。

case class Config(_id: Option[ObjectId] = None,
                            var locations: Set[Location] = Set(),
                            var languages: Set[Language] = Set(),
                            var workAreas: Set[WorkArea] = Set(),
                            var websites: Set[WebSite] = Set()) {}

类型参数也是案例类。数据库连接配置如下:

private val customCodecs = fromProviders(
    classOf[Config],
    classOf[Language],
    classOf[Location],
    classOf[JobPortal],
    classOf[WorkArea]
  )

  private val javaCodecs =
    fromCodecs(new LocalDateTimeDateCodec(), new LocalDateDateCodec())

  private val codecRegistry =
    fromRegistries(customCodecs, javaCodecs, DEFAULT_CODEC_REGISTRY)

  val dbConnection = MongoClient(dbURI)

  val database: MongoDatabase =
    dbConnection.getDatabase(dbName).withCodecRegistry(codecRegistry)

相应的集合中有一个文档。当我调用控制器端点时,得到以下堆栈跟踪:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.collection.immutable.Set]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
        at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:363)
        at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:361)
        at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:413)
        at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
        at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
        at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.collection.immutable.Set
        at repository.MongoFactory$$anon$5$ConfigMacroCodec$3.getInstance(MongoFactory.scala:20)
        at repository.MongoFactory$$anon$5$ConfigMacroCodec$3.getInstance(MongoFactory.scala:20)
        at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.decode(MacroCodec.scala:110)
        at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.decode$(MacroCodec.scala:95)
        at repository.MongoFactory$$anon$5$ConfigMacroCodec$3.decode(MongoFactory.scala:20)
        at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
        at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
        at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
        at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
        at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)

我需要一套自定义编解码器吗?

1 个答案:

答案 0 :(得分:0)

我不认为Set支持开箱即用,但Seq是支持的。参见Implicit conversions