使用Scala Mongo DB

时间:2019-02-05 01:12:24

标签: mongodb scala case-class

我正在使用Scala 2.12.6和

我有一个如下的Alert类:

case class Alert(
                  _id: org.mongodb.scala.bson.ObjectId,
                  uuid: String,
                  created: ZonedDateTime,
                  resolved: Option[ZonedDateTime],
                  status: String,
                  activity: Seq[AlertActivity]
                )

,嵌套的类如下:

sealed class AlertActivity {
}

我有一个DAO,该DAO具有保存activity为空的Seq时可以正常工作的Alert的方法,以及一种在每次更新Alert时都更新活动部分的方法,例如用户做某事。该类的相关部分如下所示:

private val customCodecs = fromProviders(
  classOf[Alert],
  classOf[AlertActivity],
  classOf[UserDetails]
)

private val javaCodecs = CodecRegistries.fromCodecs(new ZonedDateTimeStringCodec())

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

private val alerts: MongoCollection[Alert] = mongoClient.getDatabase("shepherd").withCodecRegistry(codecRegistry).getCollection("alerts")

override def getAlert(alertId: String): Future[Option[Alert]] = 
    alerts.find(equal("uuid", alertId)).first().head().map(Option(_))

override def addAlertActivity(uuid: String, activity: AlertActivity): Future[Option[Alert]] =
    alerts.updateOne(equal("uuid", uuid), push("activity", activity)).toFuture().flatMap(_ => getAlert(uuid))

如果我有一个AlertActivity的子类,则可以正常工作,例如

case class AlertCreated(timestamp: Long) extends AlertActivity {
  override def toString: String = "Alert created at " + ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.UTC)
}

但是,如果我添加第二个子类,它将开始失败。例如

case class EmailNotification(notificationId: String, timestamp: Long, userDetails: UserDetails) extends AlertActivity {
  override def toString: String = s"Email sent to ${userDetails.email} at " + ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.UTC)
}

我得到的错误是:

Error:(25, 12) No known subclasses of the sealed class
classOf[AlertActivity],

Error:(25, 12) type mismatch;
found   : Class[model.AlertActivity](classOf[model.AlertActivity])
required: org.bson.codecs.configuration.CodecProvider
classOf[AlertActivity],

或者有时测试实际上在运行,而我得到

Boxed Error
java.util.concurrent.ExecutionException: Boxed Error
at scala.concurrent.impl.Promise$.resolver(Promise.scala:83)
at scala.concurrent.impl.Promise$.scala$concurrent$impl$Promise$$resolveTry(Promise.scala:75)
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:280)
at scala.concurrent.Promise.complete(Promise.scala:49)
at scala.concurrent.Promise.complete$(Promise.scala:48)
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:183)
at scala.concurrent.Promise.failure(Promise.scala:100)
at scala.concurrent.Promise.failure$(Promise.scala:100)
at scala.concurrent.impl.Promise$DefaultPromise.failure(Promise.scala:183)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2.$anonfun$onError$1(ObservableImplicits.scala:369)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2$$Lambda$239/949581868.apply(Unknown Source)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2.completeWith(ObservableImplicits.scala:383)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2.onError(ObservableImplicits.scala:369)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3.$anonfun$onError$2(ObservableImplicits.scala:445)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3$$Lambda$238/1537772520.apply$mcV$sp(Unknown Source)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3.completeWith(ObservableImplicits.scala:462)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3.onError(ObservableImplicits.scala:445)
at org.mongodb.scala.internal.SubscriptionCheckingObserver.onError(SubscriptionCheckingObserver.scala:38)
at com.mongodb.async.client.AbstractSubscription.onError(AbstractSubscription.java:123)
at com.mongodb.async.client.AbstractSubscription.tryRequestInitialData(AbstractSubscription.java:153)
at com.mongodb.async.client.AbstractSubscription.request(AbstractSubscription.java:84)
at org.mongodb.scala.ObservableImplicits$BoxedSubscription.request(ObservableImplicits.scala:490)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2.onSubscribe(ObservableImplicits.scala:366)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3.onSubscribe(ObservableImplicits.scala:441)
at org.mongodb.scala.internal.SubscriptionCheckingObserver.onSubscribe(SubscriptionCheckingObserver.scala:28)
at org.mongodb.scala.Observer.onSubscribe(Observer.scala:85)
at org.mongodb.scala.Observer.onSubscribe$(Observer.scala:85)
at org.mongodb.scala.internal.SubscriptionCheckingObserver.onSubscribe(SubscriptionCheckingObserver.scala:21)
at com.mongodb.async.client.SingleResultCallbackSubscription.<init>(SingleResultCallbackSubscription.java:33)
at com.mongodb.async.client.Observables$2.subscribe(Observables.java:78)
at org.mongodb.scala.ObservableImplicits$BoxedObservable.subscribe(ObservableImplicits.scala:474)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable.subscribe(ObservableImplicits.scala:433)
at org.mongodb.scala.ObservableImplicits$ScalaObservable.headOption(ObservableImplicits.scala:358)
at org.mongodb.scala.ObservableImplicits$ScalaObservable.head(ObservableImplicits.scala:344)
at org.mongodb.scala.ObservableImplicits$ScalaSingleObservable.toFuture(ObservableImplicits.scala:411)
at daos.MongoAlertDao.addAlertActivity(MongoAlertDao.scala:68) <----- THIS IS THE addActivity method

我还在使用Intellij IDEA 2018.3.4(Ultimate Edition)内部版本#IU-183.5429.30 JRE:1.8.0_152-release-1343-b26 x86_64 JVM:JetBrains s.r.o的OpenJDK 64位服务器VM macOS 10.14.1

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

所以我删除了.idea文件,然后重新导入了项目,错误消失了