0

我正在开发简单的 scala web 应用程序,我正在运行一个查询,然后 play 应用程序给出了异常

[RuntimeException: UnexpectedNullableFound(ColumnName(.SUM(score),Some(SUM(score))))]

play.api.Application$$anon$1: Execution exception[[RuntimeException: UnexpectedNullableFound(ColumnName(.SUM(score),Some(SUM(score))))]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.2]
    at play.api.DefaultApplication.handleError(Application.scala:383) ~[play_2.10.jar:2.1.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$18$$anonfun$apply$18.apply(PlayDefaultUpstreamHandler.scala:310) ~[play_2.10.jar:2.1.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$18$$anonfun$apply$18.apply(PlayDefaultUpstreamHandler.scala:308) ~[play_2.10.jar:2.1.2]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.2]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.2]
Caused by: java.lang.RuntimeException: UnexpectedNullableFound(ColumnName(.SUM(score),Some(SUM(score))))
    at scala.sys.package$.error(package.scala:27) ~[scala-library.jar:na]
    at anorm.Sql$.as(Anorm.scala:535) ~[anorm_2.10.jar:2.1.2]
    at anorm.Sql$class.as(Anorm.scala:453) ~[anorm_2.10.jar:2.1.2]
    at anorm.SimpleSql.as(Anorm.scala:370) ~[anorm_2.10.jar:2.1.2]
    at models.Keyword$$anonfun$getBlogScore$1.apply(Keyword.scala:53) ~[na:na]
    at models.Keyword$$anonfun$getBlogScore$1.apply(Keyword.scala:52) ~[na:na]

模型/keyword.scala

def getBlogScore(word: String, blog: String): Long={DB.withConnection{implicit c=>
 SQL("select SUM(score) from keyword where blog like {blog} and word = {word}").on('blog->blog,'word->word).as(scalar[Long].single)
}
}

我想根据某些条件获得分数列的总和。

给我一些想法来解决这个异常!

4

3 回答 3

1

尝试:

SQL("select SUM(score) from keyword where blog like {blog} and word = {word}").
  on('blog->blog,'word->word).
  as(scalar[Option[Long]].single).
  getOrElse(0L)
于 2013-11-18T13:58:58.190 回答
0

Little bit late but this issue is fixed in Anorm master branch: https://github.com/playframework/playframework/pull/2072

于 2013-12-19T00:52:32.807 回答
0

我猜当没有匹配的行时SUM返回。NULL我也猜想0在这种情况下你可能想要。

所以你必须处理这种情况。Option想到了。快速浏览一下文档,grepping forOption表示有一个.singleOpt.

所以你可以尝试:

def getBlogScore(word: String, blog: String): Long = 
  DB.withConnection { implicit c =>
    SQL("select SUM(score) from keyword where blog like {blog} and word = {word}").
      on('blog->blog,'word->word).
      as(scalar[Long].singleOpt).
      getOrElse(0L)
  }
于 2013-10-31T13:08:58.143 回答