使用子类检索Salat对象时出现异常

时间:2013-07-04 09:32:04

标签: mongodb scala playframework-2.0 specs2 salat

我有一个具有子类作为属性的对象:

case class UpgradeInfo(
    @EnumAs upgradeType: UpgradeType.Value,
    @EnumAs timeUnit: TimeUnit.Value,
    timeQuantity: Int,
    start: Date,
    end: Date)

case class Upgrade(
    @Key("_id") id: ObjectId = new ObjectId,
    adId: ObjectId,
    upgradeInfo: UpgradeInfo,
    price: Double)

创建和更新对象工作正常,但是当我尝试使用此代码检索Upgrade对象时(应检索所有升级对象):

UpgradeDAO.find(MongoDBObject()).toList

我得到以下例外:

[error]     Exception: class models.entities.Upgrade requires value for 'upgradeInfo' (Grater.scala:402)
[error] com.novus.salat.DefaultArg.safeValue$lzycompute(Grater.scala:406)
[error] com.novus.salat.DefaultArg.safeValue(Grater.scala:402)
[error] com.novus.salat.ConcreteGrater.safeDefault(Grater.scala:363)
[error] com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
[error] com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
[error] com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
[error] com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
[error] com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
[error] com.novus.salat.dao.SalatMongoCursorBase$class.next(SalatMongoCursor.scala:45)
[error] com.novus.salat.dao.SalatMongoCursor.next(SalatMongoCursor.scala:151)
[error] com.novus.salat.dao.SalatMongoCursor.foreach(SalatMongoCursor.scala:151)
[error] com.novus.salat.dao.SalatMongoCursor.to(SalatMongoCursor.scala:151)
[error] com.novus.salat.dao.SalatMongoCursor.toList(SalatMongoCursor.scala:151)
[error] models.entities.UpgradeDAO$.find(Upgrade.scala:47)
[error] models.UpgradeSpecs$$anonfun$1$$anonfun$apply$28$$anonfun$apply$29.apply(UpgradeSpecs.scala:129)
[error] models.UpgradeSpecs$$anonfun$1$$anonfun$apply$28$$anonfun$apply$29.apply(UpgradeSpecs.scala:128)
[error] play.api.test.Helpers$.running(Helpers.scala:40)
[error] models.UpgradeSpecs$$anonfun$1$$anonfun$apply$28.apply(UpgradeSpecs.scala:128)
[error] models.UpgradeSpecs$$anonfun$1$$anonfun$apply$28.apply(UpgradeSpecs.scala:128)

知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

这是由您的mongo集合中的文档导致的,该文档缺少upgradeInfo的值。

你有几个选择:

  1. upgradeInfo
  2. 提供默认参数
  3. upgradeInfo的类型更改为Option[UpgradeInfo]并提供默认值None
  4. 搜索您的mongo集合以查找错误文档,并删除或手动修复丢失的文档upgradeInfo